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

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. The simplest reason for this error is the missing log4j.jar file. Since org.apache.log4j.Logger class belongs to this JAR file, if it's 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 the name of the class e.g. org.apache.log4j.Logger and Eclipse will show all the class files with the 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 the problem is caused due to the missing log4j.jar file then you can fix it by adding a relevant version of log4j.jar into your CLASSPATH. Now, which version of the JAR file you should add will depend upon the application and library you are using but on the safer side, you can always add the latest one. 

Btw, it's worth remembering that there is a huge difference between Log4j and Log4j2, which also provides asynchronous logging, so make sure you add the right version of the log4j library into your application's classpath.

You can download the log4j.jar file either from Maven central repository or directly from Apache Log4j website http://logging.apache.org/log4j/1.2/download.html. If you are using log4j 2 e.g. for asynchronous logging then you can download log4j2.jar from here. It exists on the same website but at a different location.

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

If you are using Maven then you can also add the following dependency in your pom.xml file to download Log4j.jar
<dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.15</version>
</dependency>
If you are using log4j version 2 then you can add the following Maven dependency :
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.3</version>
</dependency>


That's all about how to fix Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger error in Java application. In most cases, this issue will go away as soon as you add the log4j.jar file but if it still persists 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: com.microsoft.sqlserver.jdbc.SQLServerDriver Error in JDBC [solution]
  • Solution of java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [solution]

7 comments:

  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?

    ReplyDelete
  2. am also getting same error after adding the jar file

    ReplyDelete
  3. no use still getting the same error this should not happen usually but....
    ani updated solution needed for such issue

    ReplyDelete
  4. What does ctrl-t do? Do I have to be in a particular menu or something when I type it? I've tried it while just having the project selected as well as being inside the java build path menu and that does nothing but give an error tone.

    ReplyDelete
    Replies
    1. Ctrl+T open a Type which is a class in Eclipse. You can run this while you are in Java editor and it will open class files with the name you want to search, good thing is it also search all the JAR files in the classpath and workspace, so it makes it really easy to find anything

      Delete
  5. thank you it worked

    ReplyDelete
  6. i want to use slf4j instead of log4j, can you please tell me how to achieve it

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.