I am trying my first project with gradle. My project has a dependency with log4j-1.2.17.jar After my project is built, a jar file is generated. I try to run this with the following:
java -classpath ".:/home/ec2-user/dlsvr/lib/log4j-1.2.17.jar" -jar dlsvr.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at com.secutrans.dlsvr.DLSvrMain.<clinit>(DLSvrMain.java:27)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
But if I build a fatjar with gradle, the fatjar works without specifying any classpath. The dependency statement in gradle is like:
dependencies
{
compile files("/home/ec2-user/dlsvr/lib/log4j-1.2.17.jar")
}
Yes, AbtPst is correct. It is a classpath problem.
Here is similar question. Does java -jar option alter classpath options
An executable JAR must reference all the other dependent JARs it requires through the Class-Path header of the manifest file. The environment variable CLASSPATH and any class path specified on the command line is ignored by the JVM if the -jar option is used.
My problem was resolved by adding classpath in manifest file of the jar through gradle.
jar {
manifest {
attributes(
"Class-Path": configurations.compile.collect { it.getName() }.join(' '))
}
}