Disclosure: This article may contain affiliate links. When you purchase, we may earn an affiliate commission.

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

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>

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. http://repo1.maven.org/maven2/commons-logging/commons-logging/1.1.2/commons-logging-1.1.2.jar

You can even download apache commons-logging JARs from the Apache Commons Logging website as well, http://commons.apache.org/logging/download_logging.cgi as well.


That's all about how to fix java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory error in Java. 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.support.AbstractApplicationContext.(AbstractApplicationContext.java:158)
    at org.springframework.context.support.GenericApplicationContext.(GenericApplicationContext.java:102)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext.java:60)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext.java:82)
    at hello.Application.main(Application.java:21)
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown 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?

    ReplyDelete

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