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 logging framework to log messages into 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 framework like Hibernate, Spring and commons-logging-1.1.2.jar is not available in CLASSPATH. Sometime this may be due to some nasty classpath issue, but most often this is due to missing JAR in the classpath.

Solution : If its case of missing JAR file then issue 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 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 new feature which is only available in commons-logging-1.1.2.jar is triggered. That's why its 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 correct version of transitive dependent JARs are downloaded.

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


If you are using Maven in Eclipse using 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 maven central website and download commons-logging.jar file.

You can even download apache commons logging JARs from 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. It can come even if you are not using commons logging dirctly 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 commons-logging-1.1.1.jar file in your CLASSPATH. If you see this error even after having commons-logging.jar then it means your CLASSPATH is not set properly. Follow steps given in this article to setup your CLASSPATH properly.

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

Related tutorials and guide to deal with different error and exception 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]

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?