NoClassDefFoundError after updating to Java 7

2019-06-04 05:18发布

问题:

I'm getting the NoClassDefFoundError after executing my project's JAR file. Debugging with Eclipse works fine, but I get this error whenever I use the windows command java -jar myproject.jar since I installed JDK 1.7.

It was working fine using Java 1.6.

Here's the error log :

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Layout
at program.Main.main(Main.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Layout
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more

I googled about that but couldn't find the right solution.

What I already did : -uninstall Java 1.7, install 1.6 instead -check the classpath -try on an other computer (after uninstalling Java 1.7 from it) -create an empty project with one reference to the log4j library => same problem

And here's the classpath file :

<?xml version="1.0" encoding="UTF-8"?>
<classpath> 
<classpathentry kind="src" path="src"/> 
<classpathentry kind="con"path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="lib/RXTXcomm.jar"/> 
<classpathentry kind="lib" path="lib/log4j-1.2.15.jar"/> 
<classpathentry kind="output" path="bin"/> 
</classpath>

Do you have any idea what else I should do?

Thanks,

Nils

EDIT : if I export the project as an "runnable JAR" instead of a simple "JAR", the program starts but fail to use another library I'm using (RxtxComm). Here's the log I get:

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver

EDIT2: I finally resolved the last problem by putting the rxtxSerial.dll in the Windows/system32 folder. But still, I don't understand why it doesn't work anymore if I export my project as a JAR file and why I need to use the dll file now.

回答1:

Are you sure log4j's jar is in the classpath when running your jar file? Check that either:

  • you have a CLASSPATH system variable which contains, amongst others, the folder wher log4j's jar is located

  • you pass that log4j jar folder as an argument when launching the jar

    java -jar yourJar.jar -classpath c:\myLog4JJarFolder

UPDATE:

As per Andrew Thompson's comment using the -jar argument will make your -classpath argument useless (it will be ignored). To be able to specify the classpath at the command line you have to also use the fully qualified name of your main class as a launch argument:

java -classpath "c:\dir1;c:\dir2" package.subPackage.MainClass


回答2:

In addition to the above, a few things that I would check:

  • Ensure that you have packaged all of your resources in the executable JAR. Extract your jar and take a look just to make sure.

  • Add the jar to the classpath:

    java -cp [jar here] main.java