我正在尝试让一段代码与 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
知道我做错了什么吗?