I want to create a jar file from a maven java project and add this as an external jar file in another project. However I keep getting a NoClassDefFoundError error.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.tutelatechnologies.dashboard.DataUsageLogs.Logging.getlogger(Logging.java:10)
at com.tutelatechnologies.dashboard.DataUsageLogs.Logging.d(Logging.java:26)
at com.tutelatechnologies.dashboard.DataUsageLogsRunner.DUL_TESTER.main(DUL_TESTER.java:11)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 3 more
The jar file is very simple. It merely contains wrappers for logging with Log4J and SLF4J. I’ve included it blow:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Logging {
public void e(String msg, Class inputClass)
{
Logger logs = LoggerFactory.getLogger(inputClass);
logs.error(msg);
}
public void w(String msg, Class inputClass)
{
Logger logs = LoggerFactory.getLogger(inputClass);
logs.warn(msg);
}
public void d(String msg, Class inputClass)
{
Logger logs = LoggerFactory.getLogger(inputClass);
logs.debug(msg);
}
public void t(String msg, Class inputClass)
{
Logger logs = LoggerFactory.getLogger(inputClass);
logs.trace(msg);
}
}
Now I right-click this project, select Export -> Java -> Jar File then click Finish. Leaving all the folders selected.
I then go to my new project and add the jar file by right-clicking my project, select Build Path-> Configure Build Path. Then in the Libraries tab I click the “Add external Jars” button and link it to my jar file. I attach my source code and a javadoc so I can verify it’s linked correctly. It looks to be ok, but when I run it I get NoClassDefFoundError. This is because eclipse doesn’t seam to be adding in the dependencies to the jar file.
I can get this to work if I add the project to the Java Build path. But I want this to be a self contained jar file. It falls apart when It hits LoggerFactory.getLogger(inputClass); which is looking for the slf4j dependency.
The following line is the only line used in testing this.
Logging.d("test", thisClass.class);
Is it possible to get all dependencies packed into a single jar file and have it link correctly?
FYI, I’ve tried making this a Runnable Jar, which seams to work but then the jar file doesn’t know how to link to the log4j.properties file. This file is located in src/main/resources.
Any help is appreciated.
Cheers,
I’m not sure exactly what the issue was, but I was able to get it working. I exported the file, but with only the relevant source and property file selected. Then I added it to the top of the list of my other projects Order and Export tab.
Ultimately I added the jar as a maven dependency, and then added all the necessary dependencies for the jar to the new project as well, and that worked.
Cheers.