How to fix java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory

Problem: You are getting java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory in your Java program, which uses a logging framework to log messages into a log file.  It could be direct dependency or indirect dependency due to any framework e.g. Spring, Hibernate, or any open-source library like jackson or any other JSON parsing library.

Cause : java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory error comes when you are using Apache commons-logging directly or indirectly using frameworks like Hibernate, Spring and commons-logging-1.1.2.jar are not available in CLASSPATH. Sometimes this may be due to some nasty classpath issue, but most often this is due to missing JAR in the classpath.

Solution: If it's a case of a missing JAR file then issues will be solved as soon as you deploy the commons-logging-1.1.1.jar or commons-logging-1.1.2.jar in CLASSPATH, but deploying JAR manually is tedious and error-prone.

You need to first find those JAR files on the internet and then hope that there is no version mismatch error e.g. suppose your Spring or Hibernate version is using Apache commons-logging version 1.1.2 and you deployed version 1.1.1, you might not detect any error upfront but it will fail in production as soon as a new feature which is only available in commons-logging-1.1.2.jar is triggered. 

That's why it's best to use Maven to download and manage dependency JAR files. Maven will not only automatically download the JAR file for you but also it will ensure that the correct versions of transitive dependent JARs are downloaded.

In order to add commons-logging JAR using Maven, you can add the following dependency in your pom.xml


If you are using Maven in Eclipse using the M2Eclipse plugin, You can even search and add dependencies like below :

java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory Solution

For manual download, you can go to the maven central website and download the commons-logging.jar file.

And, if you are using Gradle, you can use below code to pull the dependency from repo

implementation 'commons-logging:commons-logging:1.2' // Use the latest version

You can even download apache commons-logging JARs from the Apache Commons Logging website as well, as well.

That's all about how to fix java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory error in Java. The java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory error typically occurs when the required Apache Commons Logging library is not available in the classpath during runtime. You can fix this issue by adding depending via Maven, Gradle, or manually downloading JAR file and adding classpath. 

Though, in some cases, it can come even if you are not using commons-logging directly but using a library like Spring, Hibernate, or Struts, which internally uses this library for their logging need. This error should be fixed as soon as you put the commons-logging-1.1.1.jar file in your CLASSPATH. 

Related tutorials and guides to deal with different errors and exceptions in Java :
  • How to fix java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject Error? [solution]
  • How to solve java.lang.NoClassDefFoundError: org/dom4j/DocumentException [solution
  • What is difference between ClassNotFoundException and NoClassDefFoundError in Java? [answer]
  • How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Java? [answer]
  • How to fix java.lang.unsupportedclassversionerror unsupported major.minor version 49.0 50.0 51.0 in Java [solution]
  • How to Fix java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver [solution
  • 'javac' is not recognized as an internal or external command, operable program, or batch file [solution]
If you see this error even after having commons-logging.jar then it means your CLASSPATH is not set properly. Follow the steps given in this article to set up your CLASSPATH properly.

1 comment:

  1. I am getting following exception while trying to write a simple Spring HelloWorld application using Spring 4.2.1 version using autowiring and annotation config application context.

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.(
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.(
    at hello.Application.main(
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at$ Source)
    at Method)
    at Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 5 more

    I have spring-core-4.2.1.RELEASE.jar, spring-context-4.2.1.RELEASE.jar and spring-beans-4.2.1.RELEASE.jar in my Eclipse build path as referenced libraries.

    Do I simply download latest version of apache commons logging library and put it on classpath? Or spring 4.2.1 has dependency on any specific version of Apache commons logging JAR?


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