5

I am trying to get a piece of code working with log4j. If I run it via junit tests (ant task), I am getting proper log outputs and all is fine. If I run the code from the command line, I get this:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Layout at uk.co.bytemark.flexnbd.Main.main(Main.java:29) Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Layout 

I tried the two following classpaths but neither helped. The error remains.

 export CLASSPATH=/home/yann/java/apache-log4j-1.2.16/log4j-1.2.16.jar export CLASSPATH=/home/yann/java/apache-log4j-1.2.16/ 

Even with this

$ java -cp /home/yann/java/apache-log4j-1.2.16/log4j-1.2.16.jar -jar ./dist/lib/flexnbd-`date +%Y%m%d`.jar server ::1 12345 fileSystem Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Layout at uk.co.bytemark.flexnbd.Main.main(Main.java:29) Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Layout at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) ... 1 more $ ls -ld /home/yann/java/apache-log4j-1.2.16/log4j-1.2.16.jar 476K -rw-r--r-- 1 yann yann 471K Mar 31 05:16 /home/yann/java/apache-log4j-1.2.16/log4j-1.2.16.jar 

Any idea what I am doing wrong?

4
  • I think you overwrite the classpath. try this, but not sure, so this is not a pretty answer: export CLASSPATH=$CLASSPATH:/home/yann/java/apache-log4j-1.2.16/log4j-1.2.16.jar:/home/yann/java/apache-log4j-1.2.16/ Commented Jul 16, 2010 at 9:36
  • Ok, can you write the command line please ? Commented Jul 16, 2010 at 9:41
  • yes, thx. hope my answer can help you Commented Jul 16, 2010 at 10:19
  • did you check the content of your jar ? Maybe your download went wrong and the class is missing. try unzipping it Commented Jul 16, 2010 at 10:27

1 Answer 1

8

Found the problem. If you use -jar, your CLASSPATH is ignored. A better command line is thus:

java -cp dist/lib/flexnbd-20100716.jar:/home/yann/java/apache-log4j-1.2.16/log4j-1.2.16.jar uk.co.bytemark.flexnbd.Main [...] 

Thanks for the help!

Sign up to request clarification or add additional context in comments.

1 Comment

Cool :), finally I was almost right, but I just forget to include dist/lib/flexnbd-20100716.jar in the classpath. I delete my answer, since it's not usefull you can accept your answer :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.