0

I'm coding a maven project with eclipse 2018.09 under java 11 and I've a problem with the maven jar creation. When I clean package the project, it delivers me a jar but no dependencies are added into and i sometimes have warning in eclipse like:

classpath entry junit(for example) will not be exported it may result a ClassNotFoundException.

Which is in fact what's happening when i launch my jar project.

Thanks.

  • this may help: https://stackoverflow.com/questions/1729054/including-dependencies-in-a-jar-with-maven – mangusta Nov 10 '18 at 16:56
  • 1
    do share the details of how you are building the project and what all dependencies are you including in the project. what is the exact stacktrace of your exception? – Naman Nov 10 '18 at 16:56
  • the error is about spring but it does so with all the dependencies when i launch the jar an the exact exception i ClassDefNotFoundException in the jar. –  Nov 10 '18 at 16:58
  • JUnit should never be in a final jar file cause it's only used on classpath for unit tests but never in production code...Please post full pom file and show the full error message... – khmarbaise Nov 10 '18 at 17:27
  • Yeah i know but it was for the example –  Nov 10 '18 at 17:28

2 Answers2

2

it delivers me a jar but no dependencies are added into [it]

it is totally normal. By default, when Maven builds a jar, it does not add any dependencies in it, but only the .class and resources of your current project.

When you run your programm you want it to find your dependencies otherwise you will face ClassNotFoundException. So you must configure your classpath to reference the dependencies.

1- if you want to run you programm from your local computer with Maven, use the exec Maven plugin with the <java> goal defined in your pom like explained here: https://www.mojohaus.org/exec-maven-plugin/usage.html#Java_goal

alternatively you can run it from a launcher in your IDE. The IDE will build the classpath for you and the classpath will corectly contain your dependencies.

2- if you want to run from the command line on any computer, you have to copy all of you dependencies in one directory (using Maven's dependency plugin mvn dependency:copy) and run you jar like this:

java -cp myProgram.jar:dependencyDirectory/* com.blabla.MainClass

(beware the use of ';' or ':' and '/' or '\' depending on Linux/Windows)

3- as an alternative you can run your jar with java -jar myprogram.jar but only if it contains a correct MANIFEST.MF where the location of all the dependencies are hardcoded.

My advice is to target solution 1 or 2 first.

PS: you can also create "fat jars" or "uber jars" containing your dependencies but I would advise you do not target this solution at first.

Francois Marot
  • 1,145
  • 11
  • 18
0

You can simply add this to your pom.xml (under the < plugins > tag):

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>App</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Remember to change the mainclass to your entrypoint (which class the static void main(string[args]) is). Now when you run the command mvn clean install there will be a jar in the targets folder with name yourproject-version-SNAPSHOT-jar-with-dependencies.jar

Mr.Turtle
  • 2,950
  • 6
  • 28
  • 46