我想在一個Eclipse項目中使用log4j.2.x進行日誌記錄。我的conf文件名爲log4j2.xml,它直接在java項目下,並且在classpath中有必要的jar。當我測試時,我看到下面的錯誤消息。任何人都可以告訴我這個解決方案嗎? log4j2.xml:線程「main」中的異常java.lang.NoClassDefFoundError:org/slf4j/ILoggerFactory
<?xml version="1.0" encoding="UTF-8"?> <configuration status="WARN"> <appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console> </appenders> <loggers>
<root level="error">
<appender-ref ref="Console"/>
</root> </loggers></configuration>
錯誤在控制檯:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/ILoggerFactory
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at org.apache.logging.slf4j.SLF4JLoggerContextFactory.<init>(SLF4JLoggerContextFactory.java:34)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at org.apache.logging.log4j.LogManager.<clinit>(LogManager.java:84)
at Browserbot.<clinit>(Browserbot.java:17)Caused by: java.lang.ClassNotFoundException: org.slf4j.ILoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356) ... 23 more
我的Java文件:
public class Browserbot {
static Logger logger = LogManager.getLogger(Browserbot.class.getName());
public static void main(String[] args){
logger.error("File Not Found");}}
我覺得這已經是一個重複的問題。 – Woot4Moo
[線程中的異常「main」java.lang.NoClassDefFoundError]的可能重複(http://stackoverflow.com/questions/17354790/exception-in-thread-main-java-lang-noclassdeffounderror) – Woot4Moo
它不是真的該問題的重複。這涉及到一個意想不到的(對OP)的依賴;見答案。 –