运行maven-assembly创建的可执行jar:错误:无法找到或加载主类

时间:2017-05-16 16:51:59

标签: java maven jar executable-jar maven-assembly-plugin

尝试使用maven-assembly插件创建包含所有依赖项的胖jar。尝试了很多东西,但最终找不到主类的错误。好像我撞墙了,现在在这里待了几个小时。

pom.xml摘录:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>foobar</groupId>
    <artifactId>foobar</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <outputDirectory>/home/jars/foobar/</outputDirectory>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.foobar.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>...</dependencies>
</project>

执行Maven命令:

mvn clean compile assembly:single

mvn package assembly:single

mvn clean compile package assembly:single

按预期在/home/jars/foobar/foobar-1.0-SNAPSHOT-jar-with-dependencies.jar中创建jar

MANIFEST.INF

中的

META-INF

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: user
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_131
Main-Class: com.foobar.Main

执行的Java命令:

java -jar foobar-1.0-SNAPSHOT-jar-with-dependencies.jar

java -cp foobar-1.0-SNAPSHOT-jar-with-dependencies.jar com.foobar.Main

java -jar foobar-1.0-SNAPSHOT-jar-with-dependencies.jar -cp com.foobar.Main

所有返回相同的错误:Error: Could not find or load main class com.foobar.Main

操作系统:Fedora 25

java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode

javac -version
javac 1.8.0_121

mvn --version
Apache Maven 3.3.9 (NON-CANONICAL_2016-07-01T11:53:38Z_mockbuild; 2016-07-01T17:23:38+05:30)
Maven home: /usr/share/maven
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc25.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.10.13-200.fc25.x86_64", arch: "amd64", family: "unix"

解决:

问题是源代码没有编译。无法在胖罐中找到源代码jar。好像maven假定某种包装结构。

将以下内容添加到<build>解决了问题:

<sourceDirectory>src/com/foobar</sourceDirectory> 

1 个答案:

答案 0 :(得分:0)

Maven插件重新构建了JAR for Spring-boot兼容的内容,这可能导致命令行“java -jar ...”无法正常工作。用户user2354302发现,添加src / com / foobar有帮助。