log4j and java.lang.NoClassDefFoundError: org/apache/log4j/Layout

30,633

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!

Share:
30,633
Sardathrion - against SE abuse
Author by

Sardathrion - against SE abuse

I am deeply saddened and worried by the abusing behaviour of stack exchange staff with regards to its users and have stopped all activities in this network. I urge you to do educate yourselves before it is too late. The final words… Firing mods and forced relicensing: is Stack Exchange still interested in cooperating with the community? Time line of events Sign the open letter, if you wish… I did. Nescire autem quid ante quam natus sis acciderit, id est semper esse puerum. Quid enim est aetas hominis, nisi ea memoria rerum veterum cum superiorum aetate contexitur? -- Cicero, Marcus Tullius (106-43BC)

Updated on July 17, 2020

Comments

  • Sardathrion - against SE abuse
    Sardathrion - against SE abuse almost 4 years

    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?