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

maven jar plugin配置pom.xml的mainClass报错:找不到主类求助

问题:无法找到或加载主类 StrategyExecution.java

我配置的pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Buy-Binance-new-listings-on-Gateio</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.gate</groupId>
            <artifactId>gate-api</artifactId>
            <version>6.25.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20201115</version>
        </dependency>
    </dependencies>

    <build>
        <!-- this goes within <build> -->
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!-- here we specify that we want to use the main method within the App class -->
                            <mainClass>StrategyExecution.java</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
        <!-- other properties -->
    </build>

</project>

执行以下终端命令后,无论如何设置mainClass,都会报错:Error: Could not find or load main class StrategyExecution.java

  • mvn clean package
  • java -jar target/Buy-Binance-new-listings-on-Gateio-1.0-SNAPSHOT.jar

我的项目结构中,StrategyExecution.java位于src/main/java目录下,IDE中直接运行该类可正常执行,但终端运行jar包就报错,恳请提供解决建议。


解决建议

1. 修正pom.xml中mainClass的配置

pom.xml里的mainClass不能带.java后缀,需要填写类的全限定名。由于你的StrategyExecution类在默认包下(没有子包),正确配置应为:

<mainClass>StrategyExecution</mainClass>

如果类属于某个包(比如org.example),则需写成org.example.StrategyExecution。

2. 验证MANIFEST.MF文件

执行mvn clean package后,打开生成的jar包,查看META-INF/MANIFEST.MF文件,确认Main-Class属性是否为Main-Class: StrategyExecution(无.java后缀),这是JVM识别主类的关键。

3. 确认类已被正确打包

解压生成的jar包,检查是否存在StrategyExecution.class文件:

  • 若类在默认包下,该文件应在jar根目录;
  • 若有包结构,则在对应包路径下。
    如果不存在,检查target/classes目录下是否有编译后的.class文件,确认maven编译过程是否正常。

4. 可选:改用maven-shade-plugin打包(解决依赖缺失问题)

当前使用的maven-jar-plugin仅打包自身类,依赖的第三方jar不会被包含,后续运行可能出现依赖类找不到的问题。推荐使用maven-shade-plugin生成包含所有依赖的可执行jar,配置如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>StrategyExecution</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

替换原pom中的build插件配置后,重新执行mvn clean package,运行生成的shaded jar即可。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 06:25:23