Java
ClassNotFoundException
Apache Commons
LogFactory
Java Exceptions

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

Codemia Knowledge Hub ·

This exception means the JVM could not find org.apache.commons.logging.LogFactory on the classpath: your code (or a library it uses) depends on Apache Commons Logging, but commons-logging.jar is not available at runtime. The fix is to add that JAR, usually as a Maven or Gradle dependency, and make sure every classloader that needs it can see it.

What ClassNotFoundException means

java.lang.ClassNotFoundException is a checked exception thrown when an application tries to load a class by its string name and no definition is found. Methods in the class loading mechanism such as Class.forName(), ClassLoader.findSystemClass() and ClassLoader.loadClass() throw it. It happens at runtime and indicates that the environment lacks the library or JAR that contains the class.

org.apache.commons.logging.LogFactory belongs to Apache Commons Logging, a lightweight bridge API over various logging implementations. Seeing this exception means the application is trying to use Commons Logging but the library is not in the build path or classpath.

Why the LogFactory class is missing

  1. Missing library: the most common cause. commons-logging.jar is not on the project's classpath.
  2. Incorrect dependency management: a misconfigured pom.xml or build.gradle leaves the dependency out.
  3. Classloader conflicts: in applications with multiple class loaders, one loader may load the logging library without sharing it with the others.

Example that triggers the exception

This class uses Commons Logging:

java
1import org.apache.commons.logging.Log;
2import org.apache.commons.logging.LogFactory;
3
4public class LoggingExample {
5    private static final Log log = LogFactory.getLog(LoggingExample.class);
6
7    public static void main(String[] args) {
8        log.info("This is a log message.");
9    }
10}

Run it without commons-logging.jar on the classpath and the JVM throws ClassNotFoundException.

How to fix it

1. Add the Commons Logging library

Make sure commons-logging.jar is on the classpath. If you manage libraries by hand, download the JAR from the Apache Commons Logging website. With Maven, add this to pom.xml:

xml
1<dependency>
2    <groupId>commons-logging</groupId>
3    <artifactId>commons-logging</artifactId>
4    <version>1.2</version>
5</dependency>

With Gradle, add this to build.gradle:

groovy
dependencies {
    implementation 'commons-logging:commons-logging:1.2'
}

2. Check the dependency setup

If you use a dependency management tool, confirm the configuration is correct, the local repository is up to date, and proxy settings do not block access to the repositories.

3. Check the classpath in your build environment

For manually managed projects, confirm the JARs are actually included by your IDE settings or build scripts.

4. Resolve classloader issues

In applications with multiple class loaders or a complex loading hierarchy (common in J2EE containers like Tomcat), make sure commons-logging.jar is reachable across those boundaries. This may mean placing the JAR in a shared library directory such as CATALINA_HOME/lib for Tomcat.

IssueDescriptionSolution
Missing LibraryCommons Logging JAR is not in classpathAdd commons-logging.jar to classpath
Dependency Management ErrorsMisconfigured pom.xml or build.gradleCorrect and verify dependency management setup
Classloader ConflictsDifferent class loaders cannot access the same libraryPlace JAR in shared classloader paths

What Commons Logging does

Apache Commons Logging is an abstraction layer. It does not do the logging itself but delegates to an underlying framework such as Log4j, SLF4J or the built-in java.util.logging.

Many modern applications use SLF4J (Simple Logging Facade for Java) instead, because of its simpler architecture and wide support. If you migrate, make sure all logging calls are mapped correctly.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms