执行Jar:无法加载或找到主类

时间:2017-08-08 09:41:38

标签: java maven executable-jar

我试图从我的项目中构建一个可执行jar文件 由于它是一个Maven项目,我试图像这样实现它:

./mvnw package -Pdev -DskipTests

我得到了一个jar文件,所以直到这里它才能正常工作。但是,当我想用​​:

执行jarfile时
java -jar myproject-0.0.1-SNAPSHOT-jar-with-dependencies.jar

我收到无法加载或找到主类的错误。 我已经使用Google搜索了这个问题,并将以下几行添加到我的pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.david.coinlender.CoinlenderApp</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

我已经说明了我的mainClass包和类名,然而,这并没有帮助 我认为问题可能是:在jar文件中有一个文件夹类(带编译的类) 我是否需要以某种方式为类添加属性classpathPrefix?到目前为止我只将它用于libs。

有人可以帮助我吗?

亲切的问候, 大卫

@Update: 当我打开JAR文件时,我看到的结构与我的应用程序结构完全不同。让我发布我看到的截图: folder structure of the JAR file

构建JAR文件会出现问题吗?

1 个答案:

答案 0 :(得分:0)

addClassPathclassPathPrefix用于引用JAR文件之外的本地类或JAR文件。引用已包含在JAR文件中的类不需要这些指令。

在您的JAR文件中有一个文件夹classes听起来很奇怪。类文件应该直接在顶层,例如

$ jar tf myproject-0.0.1-SNAPSHOT-jar-with-dependencies.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/david/
com/david/coinlender/
com/david/coinlender/CoinLenderApp.class

听起来您的项目不遵循Maven standard directory layout,导致类文件存储在错误的位置。这可能是你问题的原因吗?