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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3263439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:06:55  来源:igfitidea点击:

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

javalog4jclasspathnoclassdeffounderror

提问by Sardathrion - against SE abuse

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:

我正在尝试使用 log4j 获取一段代码。如果我通过 junit 测试(蚂蚁任务)运行它,我会得到正确的日志输出,一切都很好。如果我从命令行运行代码,我会得到:

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.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?

知道我做错了什么吗?

采纳答案by Sardathrion - against SE abuse

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

发现问题了。如果您使用 -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 [...]

Thanks for the help!

谢谢您的帮助!