java.lang.Noclassdeffounderror: org/slf4j/Loggerfactory

Asked

Viewed 3,972 times

3

good afternoon! I was trying to run a jar classes and came across the following mistake:

Exception in thread "main" java.lang.Noclassdeffounderror: org/slf4j/Loggerfactory

I searched and found this site : http://www.slf4j.org/

What exactly is this mistake?

4 answers

5

The mistake java.lang.NoClassDefFoundError is one of the exceptions core of Java, which occurs at runtime when a class existing could not be loaded due to the lack of another class it depends on or, for example, when a static boot block has released an exception.

That is to say, that mistake nay occurs for lack of class org.slf4j.LoggerFactory, but for lack of some other class she needs.

The log library called Simple Logging Facade for Java (SLF4J) is only a standardized interface for accessing logs, but you still need to include an implementation of logs such as Log4j or Logback, etc..

How you solve it depends on how you use your program:

  • If it is a simple program running per command line, add the required dependencies by specifying to the Java classpath with the parameter -cp.
  • If it is a web application you can use application server logs or include your log library in the folder WEB-INF/lib.

Which jars should be included in the classpath depends on which library is used in conjunction with SLF4J and what features the application uses:

  • For simple logs using only SLF4J, you can include:
    • slf4j-api-1.x.xx.jar
    • slf4j-simple-1.x.xx.jar
  • To use Log4j:
    • slf4j-api-1.x.xx.jar
    • log4j.jar
    • slf4j-log4j12-1.x.xx.jar, the integration jar between the two libraries
  • To use Logback:
    • slf4j-api-1.x.xx.jar
    • logback-classic-1.0.13.jar
    • logback-core-1.0.13.jar.
  • Excellent answer, even for me who did not know how the full meaning of the java.lang error.Noclassdeffounderror

2

The error happened because the class is missing Loggerfactory in Classpath. To fix just add the sl4j library in Classpath, which you can find here.

You can put the files: slf4j-api-1.7.13.jar and slf4j-jdk14-1.7.13.jar in the Classpath.

  • Why slf4j-jdk14? It says somewhere that she uses Java 1.4?

  • @utluiz o slf4j-Jdk14, it was because I looked at a tutorial that asked to put this version of Jdk14.

  • The slf4j-jdk14 is a "plugin" for the Java 1.4 logging framework.

0

Complementing for those who have this same mistake in the future:

This error may also occur when you are using a Library that uses slf4j to generate runtime error logs.

And this was my case, I was using the Kafka-clients library and was getting this error, I added the files: slf4j-api-1.7.13.jar and slf4j-Jdk14-1.7.13.jar in Classpath and the slf4j-related error stopped occurring and the error KAFKA "wanted to show" appeared, that in my case it was firewall between the machine that I am running my Producer, with the bootstrap server.

0

Using only SLF4J, slf4j-simple-1.7.25.jar *Just right click on JRE System... Build Path and add to JAR.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.