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

1

15

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> 
Smoothspoken answered 16/5, 2017 at 16:51 Comment(6)
Try look inside the .jar and see how does Maven strutured the files? Can you see your class? I had similar issue when I built for Spring-boot application where the plug-in has moved the file around compatible for Spring-boot but can't be run with "java -jar ..."Hocus
What's the result if you try maven install?Cornemuse
@MinhKieu That was it! Sigh! Seems like maven assumes a certain directory structure. Solved it by adding <sourceDirectory></sourceDirectory>. If you can answer the question, I'll mark it as the correct one.Smoothspoken
@Pete thanks for answering. Your OP gave the hint.Smoothspoken
Thanks @user2354302!Hocus
What is the build command you are using after making these changes? mvn clean install? mvn package? Or something else?Fin
H
2

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.

Hocus answered 17/5, 2017 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.