How To Cook Exception Inward Thread Primary Java.Lang.Noclassdeffounderror: Org/Slf4j/Loggerfactory Inward Java

Advertisement

Masukkan script iklan 970x90px

How To Cook Exception Inward Thread Primary Java.Lang.Noclassdeffounderror: Org/Slf4j/Loggerfactory Inward Java

Sabtu, 28 Maret 2020

This fault agency your code or whatever external library you lot are using inwards your application is using the SLF4J library, an opened upward rootage logging library, but it is non able to uncovering the required JAR file e.g. slf4j-api-1.7.2.jar hence it's throwing Exception inwards thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory. If you lot hold back at the error, you lot volition meet that it's proverb it is non able to uncovering the degree org/slf4j/LoggerFactory or org.slf4j.LoggerFactory. The bundle lift indicates that it's purpose of SLF4j, so you lot bespeak SLF4j JAR files e.g. slf4j-api-1.7.2.jar inwards your application's classpath. So, larn ahead in addition to download the JAR file from SLFj website or from Maven Central repository in addition to restart your application.

Btw, the SLF4j is non genuinely a logging API but it provides abstraction over other logging libraries e.g. Log4j, java.util.logging, or LogBak. It's similar to the commons-logging library but it doesn't mess upward similar that inwards a complex surround due to improve design.

By using SLF4j, you lot tin switch to whatever logging library without changing a unmarried line of piece of employment of code inwards your application e.g. you lot switch to Log4j from java.util.logging or LogBack.

Hence, apart from SLF4j binaries, you lot also bespeak proper logging binaries e.g. log4j-1.2.16.jar or logback-1.2.3.jar if you lot are using LogBack library. Those are the libraries which volition hold upward called past times SLF4j for doing actual work.

Sometime, you lot larn this fault when your code is non genuinely using SLF4j but you lot are using a tool or library, which internally using it.


For example, I was using log4jdbc.jar, a tool to log SQL statements in addition to their timing spell running JDBC code in addition to it give me this error:

Exception inwards thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at net.sf.log4jdbc.Slf4jSpyLogDelegator.<init>(Slf4jSpyLogDelegator.java:45)
at net.sf.log4jdbc.SpyLogFactory.<clinit>(SpyLogFactory.java:37)
at net.sf.log4jdbc.DriverSpy.<clinit>(DriverSpy.java:106)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at Testing.main(Testing.java:15)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... vi more

Since my programme was uncomplicated in addition to I wasn't genuinely using whatever Logging API, I was surprised from where does this fault came in, but, a quick hold back to the stack describe suggested that its the net.sf.log4jdbc.DriverSpy degree which is using SLF4j for logging.

Later when I read the documentation of log4jdbc, I realize nosotros bespeak both SLF4J in addition to Log4J JAR files to larn it working. Since I was running my essay programme inwards Eclipse, it was tardily for me, I just dropped the slf4j-api-1.7.2.jar in addition to log4j-1.2.16.jar inwards my projection directory. Since it's included inwards classpath it got picked upward easily.


Btw, classpath problems are non tardily to solve for many Java programmers because they don't know where precisely to set this JAR file to solve their problem. Since every application setup is different, I'll endeavor to encompass about scenarios to solve this error.

1. If you lot are running your Java programme using a batch script or trounce script, hold back for -cp or -classpath selection in addition to meet where it is picking the JAR files. You tin set slf4j-api-1.7.2.jar in addition to log4j-1.2.16.jar on those directories.

If you lot programme is running on Linux server, you lot tin but just produce ps -ef | grep java in addition to meet the JVM arguments of your Java programme to uncovering which directories are inwards the classpath. If you lot convey access to the script you lot tin also add together a novel directory to the classpath.

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


2. If your programme is using CLASSPATH surround variable in addition to so you lot bespeak to just echo $CLASSPATH in addition to set the slf4j-API-1.7.2.jar in addition to log4j-1.2.16.jar files into the directory already acquaint inwards the CLASSPATH or you lot tin just add together a novel directory into the CLASSPATH. See this article to know how to brand a modify inwards CLASSPATH surround variable.


3. If you lot are using Eclipse in addition to so just drib the slf4j-API-1.7.2.jar in addition to log4j-1.2.16.jar into your projection directory. It is past times default inwards the classpath so the JARs volition hold upward picked upward past times your application.



How to download SLF4j in addition to Log4j JAR file

You tin either download slf4j-api-1.7.2.jar in addition to log4j-1.2.16.jar from respective websites e.g. https://www.slf4j.org and https://logging.apache.org/log4j/1.2/download.html or just download them from the Maven Central repository.

If you lot are using Maven for edifice your projection in addition to managing dependencies, you lot tin also add together next Maven dependency to download SLF4J in addition to Log4j JAR files into your project:

<!-- SLF4J API -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.2</version>
</dependency>

<!-- LOG4J -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.2.16</version>
</dependency>


Once you lot add together these dependencies, brand certain you lot produce a create clean construct from Maven to genuinely download these dependencies from Maven's remote repository.


That's all nigh how to develop Exception inwards thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory fault inwards Java. All you lot bespeak to produce is add together the slf4j-API-1.7.2.jar in addition to log4j-1.2.16.jar files into your classpath.

The version may differ depending upon private cases but you lot must add together a compatible version of SLF4J in addition to the logging library you lot are using. For example, if your application is using LogBack you lot bespeak to add together a relevant version of the logback.jar file.

If you lot utilisation Maven, you lot tin also download these JAR files past times adding relevant dependencies inwards pom.xml otherwise, you lot tin just download the JAR file from Maven fundamental or straight from SLF4j in addition to Log4j website in addition to add together to your application's classpath.

If you lot uncovering whatever problem adding SLF4J in addition to LOG4j JAR files into classpath you lot tin also enjoin us inwards the comment department in addition to nosotros tin endeavor to tending you lot out.

Other Java troubleshooting Guides you lot may like
How to solve ArrayIndexOutOfBoundsException inwards Java? (guide)
How to solve NullPointerException inwards Java? (guide)
How to solve the "System cannot uncovering the Path specified" error? (solution)
How to solve NoClassDefFoundError spell running Java programme from a ascendence line? (solution)
How to solve "No JVM found, Please install 64-bit JDK" fault inwards Android Studio? (solution)
How to bargain amongst SQLException "No Suitable driver constitute " fault inwards JDBC in addition to MySQL? (guide)
How to solve NumberFormatException inwards Java? (guide)
How to solve Minecraft - java.lang.UnsatisfiedLinkError: lwjgl64.dll : Access Denied? (solution)
How to develop java.lang.ArrayIndexOutOfBoundsException: one inwards Java? (solution)
How to develop java.net.SocketException: Software caused connectedness abort: recv failed (fix)

Thanks for reading this tutorial, if you lot similar this tutorial in addition to so delight percentage amongst your friends in addition to colleagues.  If you lot convey whatever inquiry or proposition in addition to so delight drib a comment.