Reputation: 5398
I am trying to run my application from the command line - I do the following - build the application using mvn clean verify
which produces a jar to which i run the jar in the normal way java -jar name.jar
I have log4j configured in my pom.xml
as follows;
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
And I have setup the application to create a manifest as seen below;
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.me.main.Main</mainClass>
</manifest>
</archive>
</configuration>
However, when I run the jar I keep getting the following exception -
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at com.me.main.Main.<clinit>(Main.java:23)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Why does maven not download the dependancy and put it in to the relevant location? What am i missing potentially from my project setup?
Thanks
Upvotes: 0
Views: 1783
Reputation: 43661
Maven just builds your JAR and manages dependencies in the build time. It's a build tool after all. if is your job to take care of dependencies in the runtime.
I think you have two options for this.
One is the Class-Path
setting in the manifest. I have never used it but I thing you'll have to do something like:
Class-Path: log4j-1.2.7.jar
An the java -jar your.jar should work as long as log4j-1.2.7.jar
is placed next to it. As I said, I have never tried this, have no idea if the syntax is right, don't trust me here.
If you want to go this way, you might think about preparing a zip assembly, otherwise people will get crazy trying to figure out how to call your application.
Another options would be to use the maven-shade-plugin
. Would be something like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.me.main.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
You'll get a big JAR with everything inside.
I personally think it is a way better option. But be very careful with licenses. IANAL but check redistribution clauses.
Upvotes: 2