Reputation: 702
I have created a maven java project which I finally want to make jar file.Let say my project name is mycustomjar and my final jar file is mycustomjar.jar
mycustomjar project uses some other dependencies or jar files let say log4J
Now when I import mycustomjar.jar file to some other java project, then it shows ClassNotFoundException for log4J classes.
Initialy I was doing this with maven dependency in pom.xml but this was not importing dependencies in jar, so I removed dependencies from pom.xml and imported all required jars(e.g: log4j-1.2.17.jar) directly to mycustomjar project. Even then problem was not saved.
I am using STS and when trying to make mycustomjar.jar it has two options
(1)Export -> jar file
(2)Export -> runnable jar file
Currently nothing working for me.Please help.
Upvotes: 0
Views: 1293
Reputation: 5749
Solution 1:
You can use a maven dependency plugin in the pom.xml file and then refer the dependencies in your classpath.
In your pom.xml
<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 then when you start the application refer it like this :
java -cp .:lib/*:your_app.jar com.yourpackage.Main
Solution 2:
Use maven assembly plugin and build a big fat jar which will include all your dependency classes into one single jar
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.build.finalName}-lib</finalName>
<appendAssemblyId>false</appendAssemblyId>
<classifier>lib</classifier>
</configuration>
</plugin>
And run the main class from this big fat jar.
java -cp your_app.jar com.yourpackage.Main
Upvotes: 1