Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger in Java

Problem : You are getting Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger error in your Java application, which is using Log4j Logger either directly or indirectly via some popular Java framework like Spring, Struts or Hibernate.

Cause : Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger error indicates that JVM is not able to found org.apache.log4j.Logger class in your application's CLASSPATH. Simplest reason of this error is missing log4j.jar file. Since org.apache.log4j.Logger class belongs to this JAR file, if its not available at run-time then your program will fail. Similar issue is java.lang.NoClassDefFoundError org.apache.log4j.Appender, which also comes due to missing log4j.jar in CLASSPATH. You can check that org.apache.log4j.Appender also belongs to log4j.jar file. If you don't know how then let me tell you a quick tip. If you have your project setup then you can simply type Ctrl + T and name of class e.g. org.apache.log4j.Logger and Eclipse will show all the class file with same name along with the JAR file on which they are packaged. I often use this trick to find out whether a particular class is present in CLASSPATH or not.

Solution: If problem is caused due to missing log4j.jar file then you can fix it by adding relevant  version of log4j.jar into your CLASSPATH. Now, which version of JAR file you should add will depend upon your application and library you are using but at safer side you can always add latest one. BTW, its worth remembering that there is huge difference between Log4j and Log4j2, which also provides asynchronous logging, so make sure you add right version of log4j library into your application's classpath.

You can download log4j.jar file either from Maven central repository or directly from Apache Log4j website If you are using log4j 2 e.g. for asynchronous logging then you can download log4j2.jar from here. It exists in same website but at different location.

Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger
If you are using Maven then you can also add following dependency in your pom.xml file to download Log4j.jar
If you are using log4j version 2 then you can add following Maven dependency :

That's all about how to fix Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger error in Java application. In most of the cases, this issue will go away as soon as you add log4j.jar file but if it still persist then you need to carefully check your CLASSPATH setting. You can see here to learn more about how to deal with CLASSPATH intricacies in Java.

Other Related errors and exceptions from Java programs :
  • How to fix java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory [solution]
  • How to solve java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject [solution]
  • Could not create the Java virtual machine Invalid maximum heap size: -Xmx [solution]
  • How to fix java.lang.unsupportedclassversionerror unsupported major.minor version 49.0 50.0 [fix]
  • How to deal with java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Java [solution]
  • How to solve java.lang.classnotfoundexception oracle.jdbc.driver.oracledrive in JDBC [solution]
  • How to fix java.lang.NoClassDefFoundError: org/dom4j/DocumentException in Apache POI [fix]
  • How to solve java.lang.ClassNotFoundException: org.Springframework.Web.Context.ContextLoaderListener in Spring [solution]
  • Solution of java.lang.ClassNotFoundException: Error in JDBC [solution]
  • Solution of java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [solution]

1 comment:

  1. hello, we are getting both "Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/Logger" and
    "Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger", I have tried adding log4j.jar file but still getting the error, don't know what is the root cause?