Error java.lang.NoClassDefFoundError: org / slf4j / LoggerFactory

3

Good afternoon! I was trying to run classes from a jar and I came across the following error:

  

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

I researched and found this site: link

What exactly is this error?

    
asked by anonymous 17.12.2015 / 19:43

4 answers

5

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

That is, this error does not occur for lack of class org.slf4j.LoggerFactory , but for lack of any other class it needs .

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

How you resolve depends on how you use your program:

  • If it is a simple program running by command line, add the necessary 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 WEB-INF/lib folder.

Which jars should be included in the classpath depends on which library is used in conjunction with SLF4J and which 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 .
18.12.2015 / 02:10
2

The error occurred because the class LoggerFactory is missing.  in the Classpath. To correct you 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 Classpath.

    
17.12.2015 / 19:50
0

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

    
20.12.2017 / 20:25
0

Complementing for anyone who has this same error in the future:

This error can also occur when you are using a library that uses slf4j to generate run-time 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 error related to slf4j stopped occurring and the error that KAFKA "wanted to show" appeared, which in my case was a firewall between the machine that I am running my producer, with the Bootstrap Server.

    
04.07.2018 / 01:39