Running executable jar created by maven-assembly: Error: Could not find or load main class

Running executable jar created by maven-assembly: Error: Could not find or load main class

正在尝试使用 maven-assembly 插件创建包含所有依赖项的 fat jar。尝试了很多东西,但最终出现 main class not found 错误。好像碰壁了,在这里待了几个小时了。

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.INFMETA-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

所有return同样的错误:Error: Could not find or load main class com.foobar.Main

OS: 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"

已解决:

问题是源代码没有被编译。在 fat jar 中找不到源代码 jar。似乎 Maven 假定了某种包结构。

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

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

Maven 插件确实重组了 JAR 的内容以兼容 Spring-boot,这可能会导致命令行 "java -jar ..." 无法正常工作。正如用户 user2354302 发现的那样,添加 src/com/foobar 有帮助。