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

user2354302 :

Trying to create a fat jar including all the dependencies using maven-assembly plugin. Tried a bunch of things, but end up with main class not found error. Seems like I've hit a wall, been here for a few hours now.

pom.xml snippet:

<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 commands executed:

mvn clean compile assembly:single

mvn package assembly:single

mvn clean compile package assembly:single

Creates jar in /home/jars/foobar/foobar-1.0-SNAPSHOT-jar-with-dependencies.jar as expected

MANIFEST.INF in 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 commands executed:

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

All return the same error: 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"

SOLVED:

The issue was that the source code wasn't getting compiled. Couldn't find the source code jar in the fat jar. Seems like maven assumes a certain package structure.

Adding the following to <build> solved the issue:

<sourceDirectory>src/com/foobar</sourceDirectory> 
Minh Kieu :

The Maven plugin does restructured the content of the JAR for Spring-boot compatible which can cause the commandline "java -jar ..." not to work properly. As user user2354302 discovered, adding src/com/foobar helped.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=469828&siteId=1