Sunday, April 28, 2024
HomeJavaHow you can repair Exception in thread "essential" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory in Java

How you can repair Exception in thread “essential” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory in Java


This error means your code or any exterior library you’re utilizing in your utility is utilizing the SLF4J library, an open supply logging library, however it isn’t capable of finding the required JAR file e.g. slf4j-api-1.7.2.jar therefore it is throwing Exception in thread “essential” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory. Should you have a look at the error, you will notice that it is saying it isn’t capable of finding the category org/slf4j/LoggerFactory or org.slf4j.LoggerFactory. The bundle identify signifies that it is a part of SLF4j, therefore you want SLF4j JAR information e.g. slf4j-api-1.7.2.jar in your utility’s classpath. So, go forward and obtain the JAR file from SLFj web site or from Maven Central repository and restart your utility.

Btw, the SLF4j will not be actually a logging API but it surely supplies abstraction over different logging libraries e.g.
Log4j, java.util.logging, or LogBak. It is just like the commons-logging library but it surely would not mess up like that in a fancy atmosphere on account of higher design.

By utilizing SLF4j, you may change to any logging library with out altering a single line of code in your utility e.g. you turn to Log4j from java.util.logging or LogBack.

Therefore, other than SLF4j binaries, you additionally want correct logging binaries e.g. log4j-1.2.16.jar or logback-1.2.3.jar in case you are utilizing LogBack library. These are the libraries which will probably be known as by SLF4j for doing precise work.

Someday, you get this error when your code will not be actually utilizing SLF4j however you’re utilizing a software or library, which internally utilizing it.

For instance, I used to be utilizing log4jdbc.jar, a software to log SQL statements and their timing whereas working JDBC code and it give me this error:

Exception in thread “essential” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at web.sf.log4jdbc.Slf4jSpyLogDelegator.<init>(Slf4jSpyLogDelegator.java:45)
at web.sf.log4jdbc.SpyLogFactory.<clinit>(SpyLogFactory.java:37)
at web.sf.log4jdbc.DriverSpy.<clinit>(DriverSpy.java:106)
at java.lang.Class.forName0(Native Methodology)
at java.lang.Class.forName(Class.java:264)
at Testing.essential(Testing.java:15)
Brought on by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.web.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at solar.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
… 6 extra

Since my program was easy and I wasn’t actually utilizing any Logging API, I used to be stunned from the place does this error got here in, however, a fast look to the stack hint urged that its the web.sf.log4jdbc.DriverSpy class which is utilizing SLF4j for logging.

Later once I learn the documentation of log4jdbc, I understand we’d like each SLF4J and Log4J JAR information to get it working. Since I used to be working my check program in Eclipse, it was straightforward for me, I simply dropped the slf4j-api-1.7.2.jar and log4j-1.2.16.jar in my undertaking listing. Because it’s included in classpath it received picked up simply.

Btw, if you do not know how SLF4j work with completely different logging library and what JAR information it’s good to get it working, here’s a good diagram which supplies full overview of how SLF4j works with Lof4j, Java Logging API in addition to Logback

Btw, classpath issues will not be straightforward to unravel for a lot of Java programmers as a result of they do not know the place precisely to place this JAR file to unravel their drawback. Since each utility setup is completely different, I am going to attempt to cowl some eventualities to unravel this error.

1. In case you are working your Java program utilizing a batch script or shell script, search for -cp or -classpath possibility and see the place it’s selecting the JAR information. You may put slf4j-api-1.7.2.jar and log4j-1.2.16.jar on these directories.

Should you program is working on Linux server, you may merely simply do ps -ef | grep java and see the JVM arguments of your Java program to search out which directories are within the classpath. When you have entry to the script you too can add a brand new listing to the classpath.

$ ps -ef | grep java
/choose/jre/v1.7.0_80-64bit/bin/java -Xmx8192M -Xms8192M 
-classpath /app/myapp.jar:/app/jackson.jar MyApplication

2. In case your program is utilizing CLASSPATH atmosphere variable then it’s good to simply echo $CLASSPATH and put the slf4j-API-1.7.2.jar and log4j-1.2.16.jar information into the listing already current within the CLASSPATH or you may simply add a brand new listing into the CLASSPATH. See this text to know find out how to make a change in CLASSPATH atmosphere variable.

3. In case you are utilizing Eclipse then simply drop the slf4j-API-1.7.2.jar and log4j-1.2.16.jar into your undertaking listing. It’s by default within the classpath so the JARs will probably be picked up by your utility.

How you can obtain SLF4j and Log4j JAR file

You may both obtain slf4j-api-1.7.2.jar and log4j-1.2.16.jar from respective web sites e.g. https://www.slf4j.org and https://logging.apache.org/log4j/1.2/obtain.html or simply obtain them from the Maven Central repository.

In case you are utilizing Maven for constructing your undertaking and managing dependencies, you too can add following Maven dependency to obtain SLF4J and Log4j JAR information into your undertaking:


<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <model>1.7.2</model>
</dependency>


<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <model>1.2.16</model>
</dependency>

When you add these dependencies, ensure you do a clear construct from Maven to truly obtain these dependencies from Maven’s distant repository.

That is all about find out how to repair Exception in thread “essential” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory error in Java. All it’s good to do is add the slf4j-API-1.7.2.jar and log4j-1.2.16.jar information into your classpath.

The model might differ relying upon particular person circumstances however you will need to add a suitable model of SLF4J and the logging library you’re utilizing. For instance, in case your utility is utilizing LogBack it’s good to add a related model of the logback.jar file.

Should you use Maven, you too can obtain these JAR information by including related dependencies in pom.xml in any other case, you may simply obtain the JAR file from Maven central or instantly from SLF4j and Log4j web site and add to your utility’s classpath.

Should you discover any hassle including SLF4J and LOG4j JAR information into classpath you too can inform us within the remark part and we are able to strive that will help you out.

Different Java troubleshooting Guides you could like

  • How you can remedy ArrayIndexOutOfBoundsException in Java? (information)
  • How you can remedy NullPointerException in Java? (information)
  • How you can remedy the “System can not discover the Path specified” error? (resolution)
  • How you can remedy NoClassDefFoundError whereas working Java program from a command line? (resolution)
  • How you can remedy “No JVM discovered, Please set up 64-bit JDK” error in Android Studio? (resolution)
  • How you can cope with SQLException “No Appropriate driver discovered ” error in JDBC and MySQL? (information)
  • How you can remedy NumberFormatException in Java? (information)
  • How you can remedy Minecraft – java.lang.UnsatisfiedLinkError: lwjgl64.dll : Entry Denied? (resolution)
  • How you can repair java.lang.ArrayIndexOutOfBoundsException: 1 in Java? (resolution)
  • How you can repair java.web.SocketException: Software program brought on connection abort: recv failed (repair)

Thanks for studying this tutorial, when you like this tutorial then please share with your folks and colleagues.  When you have any query or suggestion then please drop a remark.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments