Srikanth Nakka
Srikanth Nakka

Reputation: 758

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/ILoggerFactory

I am trying to use log4j.2.x in an Eclipse project for logging. My conf file named as log4j2.xml is directly under java project and I have necessary jars in classpath. When I test, I see below error messages. Can anybody tell me the solution for this? log4j2.xml:

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

Errors in Console:

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

My Java File:

public class Browserbot {
static Logger logger = LogManager.getLogger(Browserbot.class.getName());

public static void main(String[] args){

    logger.error("File Not Found");}}       

Upvotes: 3

Views: 71516

Answers (6)

ajaykumar kasam
ajaykumar kasam

Reputation: 21

stop adding additional jar files. infact have only two required jars 1. log4japi.jar 2. log4j-core.jar

this resolves the issue

Upvotes: 2

Vasu Kargi
Vasu Kargi

Reputation: 139

This fix is specific for web applications:

I ran into a similar problem with the NoClassDefFoundError during shutdown. In addition to the jar files listed by mojo, I also had to include log4j-web-xx.jar and that fixed the problem.

Explanation can be found here: http://logging.apache.org/log4j/2.x/manual/webapp.html

Upvotes: 0

mojo
mojo

Reputation: 51

I use the following:

slf4j-api-1.7.5.jar
slf4j-ext-1.7.5.jar
log4j-slf4j-impl-2.0-beta9.jar
log4j-core-2.0-beta9.jar
log4j-api-2.0-beta9.jar

Upvotes: 2

SContributor
SContributor

Reputation: 11

Include only relevant log4j jar files (version 2.) , that would resolve this error. In my case for the same error that has mentioned in question on log4j, I had removed all of the log4j jar files except :"log4j-api-2.0-beta9.jar" and "log4j-core-2.0-beta9.jar",this has resolved the problem and worked as expected. Error is not informative and I ended up with downloading many other jar files, but of course that did not work. Including only relavant jars should resolve this problem.

Upvotes: 0

Mercenary
Mercenary

Reputation: 2166

This could be because your classpath has not been added correctly! Make sure you use the right log4j jar in your classpath. Which version are you using? I am using log4j-1.2.15 and it works fine!

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

You are missing slf4j.jar in your classpath while running the program. To understand the dependency between slf4j and log4j, to download the jar, etc, follow this link:

http://www.slf4j.org

Upvotes: 9

Related Questions