2

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

AbtPst
  • 7,778
  • 17
  • 91
  • 172

1 Answers1

1

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

LetsBeFrank
  • 774
  • 11
  • 31
  • thanks but when i explode the jar, i can see the lib folder – AbtPst Jul 25 '17 at 13:43
  • plus i have `.` – AbtPst Jul 25 '17 at 13:44
  • should this not have included everything under the root path as classpath? – AbtPst Jul 25 '17 at 13:45
  • 1
    See official oracle doc. http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html. I didn't notice you were trying to include JARs themselves. You may be able to "hack" this to work, but I wouldn't recommend it. Most of us just make fat jars (essentially unzips all jars and places the classes into one). A maven task works well for this: https://stackoverflow.com/questions/16222748/building-a-fat-jar-using-maven – LetsBeFrank Jul 25 '17 at 16:02
  • thanks but i cant get any of those to work. all i want is to include the 3rd party jar in my classpath and package it with the uber jar. i cant figure out why this is so hard in maven – AbtPst Jul 25 '17 at 17:38