1

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.

Nils De Winter
  • 1,370
  • 3
  • 11
  • 20

2 Answers2

2

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
Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103
  • 1
    `java -jar yourJar.jar -classpath c:\myLog4JJarFolder` The `-classpath` option is ignored if `-jar` is specified. – Andrew Thompson Sep 11 '12 at 13:47
  • Doh, you're right, I'll update my anwer but also leave the original for other geniuses like me. – Shivan Dragon Sep 11 '12 at 13:50
  • I did as you said. I went into the project's bin folder and launched the class : java -classpath "C:\Documents and Settings\Nils\workspace\Transcodeur2.1\src>java -classpath "C:\Program Files\Java\jdk1.6.0_34\lib\log4j-1.2.15.jar" fr.sncf.transcodeur.Main but i get this : Exception in thread "main" java.lang.NoClassDefFoundError: fr/sncf/transcodeur/Main I also tried adding the ".class" file extension (.. .Main.class), it didn't change a thing. Btw, I'm not very comfortable using the console (that's what you get working with an IDE..!), so I might have misunderstood you. – Nils De Winter Sep 12 '12 at 09:23
0

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

Clayton Selby
  • 1,225
  • 1
  • 11
  • 26
  • I checked the JAR file, everything is in there ("lib" folder with all the librairies + .class files). By executing the java -cp command, I get the error : Exception in thread "main" java.lang.NoClassDefFoundError: Transcodeur2/1\src\fr\sncf\transcodeur\Main/java Caused by: java.lang.ClassNotFoundException: Transcodeur2.1\src\fr\sncf\transcodeur\Main.java The path should be right, since I copy/pasted it from the windows explorer – Nils De Winter Sep 12 '12 at 09:32
  • I need two things. 1: The present working directory you are running the java command from and 2: the command you are typine (e.g. java -cp /src/main/lib/myjar.jar main.java – Clayton Selby Sep 13 '12 at 18:21