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

我试图让一段代码与log4j一起工作.如果我通过junit测试(ant任务)运行它,我得到正确的日志输出,一切都很好.如果我从命令行运行代码,我得到这个:

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

我尝试了以下两个类路径,但都没有帮助.错误仍然存​​在.

  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/

即便如此

$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

知道我做错了什么吗?

解决方法:

发现了问题.如果使用-jar,则忽略CLASSPATH.因此,更好的命令行是:

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 [...]

谢谢您的帮助!

上一篇:java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment


下一篇:如何在Java应用程序中使用Akka Actors?