AbtPst
AbtPst

Reputation: 8018

java.lang.NoClassDefFoundError in uber jar

I am creating a jar with dependencies. Here is the relevant section in my POM

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>${project.basedir}</directory>
            <includes>
                <include>lib/*.jar</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>path.to.main.Main</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>.</Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> 
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal> 
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

i can build the jar just fine with mvn clean install. however when i run it

java -jar myProject-0.0.1-SNAPSHOT-jar-with-dependencies.jar, i get a java.lang.NoClassDefFoundError on a class which comes from the jar within the lib folder.

Note that i had included this jar by

        <resource>
            <directory>${project.basedir}</directory>
            <includes>
                <include>lib/*.jar</include>
            </includes>
        </resource>

so why am i getting the error? what am i doing wrong? Also, when i unzip the jar, i see the lib folder and the jar within. So why cant the myProject-0.0.1-SNAPSHOT-jar-with-dependencies.jar find it?

Update

i added

          <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

and on mvn clean install i see this on the console

[INFO] Copying my3rdParty.jar to path/to/project/target/lib/biomedical-my3rdParty-0.0.1.jar

Upvotes: 2

Views: 1313

Answers (1)

LetsBeFrank
LetsBeFrank

Reputation: 782

Check your inclusions in the built JAR. Typically you'll run into this if you have a duplicate of this class included on your classpath (in your fat JAR). See if the class in question was double-included. Either that, or what the above user said (it's not structured correctly in the built JAR).

Upvotes: 1

Related Questions