You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Eclipse中Maven Servlet项目生成可执行Jar及运行类的方法

核心问题说明

main method not found报错的根本原因是:HttpServlet是Servlet容器(如Tomcat)托管的组件,本身不包含main方法,普通Servlet项目直接打可执行jar没有程序入口点。要实现jar包直接运行,需要在项目中引入嵌入式Tomcat,编写独立的main方法启动Tomcat并注册你的Servlet。

嵌入式Tomcat集成与main方法编写

首先在pom.xml中引入嵌入式Tomcat依赖:

<dependencies>
    <!-- Servlet API -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    <!-- 嵌入式Tomcat依赖 -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>9.0.65</version>
    </dependency>
</dependencies>

新增主类编写启动逻辑,示例如下:

import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;

public class AppMain {
    public static void main(String[] args) throws Exception {
        Tomcat tomcat = new Tomcat();
        // 设置服务监听端口
        tomcat.setPort(8080);
        // 设置根上下文路径
        Context context = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
        // 注册自定义Servlet,设置映射路径
        Tomcat.addServlet(context, "serviceServlet", new service());
        context.addServletMappingDecoded("/service", "serviceServlet");
        // 启动服务并阻塞等待请求
        tomcat.start();
        tomcat.getServer().await();
    }
}

原有Servlet类逻辑无需修改,直接复用即可。

Eclipse中Maven打包可执行Jar配置

使用maven-assembly-plugin插件生成带全部依赖的可执行jar,pom.xml新增插件配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <!-- 替换为你自己主类的全限定名 -->
                        <mainClass>com.yourproject.AppMain</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Eclipse中打包操作步骤:

  • 右键项目 → 选择「Run As」→ 「Maven build」
  • 在Goals输入框填写clean package,点击运行
  • 打包完成后在项目target目录下生成后缀为jar-with-dependencies.jar的可执行文件
  • 执行命令java -jar Task-jar-with-dependencies.jar即可启动服务,启动后可通过Postman访问对应接口
通过Maven Profile导出可执行Jar

如果需要区分普通打包和可执行jar打包场景,可通过Profile实现按需打包,pom.xml新增配置:

<profiles>
    <profile>
        <id>executable-jar</id>
        <build>
            <plugins>
                <!-- 将上述maven-assembly-plugin配置移到此处 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>3.6.0</version>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>com.yourproject.AppMain</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

需要生成可执行jar时,在Eclipse的Maven build Goals中输入clean package -P executable-jar即可触发对应Profile生成可执行文件,不加-P参数时为普通打包逻辑。

运行Jar中指定类的方法

如果不想在打包时固定主类,可在运行时直接指定要启动的类,命令如下:
java -cp Task-jar-with-dependencies.jar com.yourproject.AppMain
该方式无需在pom中配置manifest主类,可灵活切换运行的入口类。

内容的提问来源于stack exchange,提问作者samkunju

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 10:45:03