"Fixing Java: NoClassDefFoundError - org/apache/commons/logging/LogFactory"

If you've stumbled upon the error java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory, you're not alone—it's a classic classpath issue that has haunted Java developers since the early 2000s. This error typically surfaces when your application tries to load the Apache Commons Logging library at runtime, but the JVM simply can't find the required class. Whether you're building a standalone app, deploying to a servlet container, or debugging a complex Maven project, understanding this error will save you hours of head-scratching.

[Java] NoClassDefFoundError Caused by: java.lang.ClassNotFoundException ...
[Java] NoClassDefFoundError Caused by: java.lang.ClassNotFoundException ...

What Is the NoClassDefFoundError?

♨️ 9.7 ERROR: NoClassDefFoundError: org/apache/commons/logging ...
♨️ 9.7 ERROR: NoClassDefFoundError: org/apache/commons/logging ...

Before diving into the specific error with org/apache/commons/logging/LogFactory, let’s clarify what NoClassDefFoundError actually means. Unlike ClassNotFoundException, which occurs when the JVM is explicitly asked to load a class that doesn’t exist on the classpath, NoClassDefFoundError is thrown when a class was present during compilation but is missing at runtime. In other words, your code compiled successfully, but something went wrong when you tried to run it.

This distinction matters because it narrows down the troubleshooting scope. The issue isn’t in your source code—it’s in how your application is packaged, deployed, or configured. The missing class in this case is part of Apache Commons Logging (also known as JCL), a lightweight logging abstraction used by many legacy libraries.

NoClassDefFoundError: org/apache/commons/logging/LogFactory · Issue ...
NoClassDefFoundError: org/apache/commons/logging/LogFactory · Issue ...

Why LogFactory Disappears at Runtime

Missing JAR in the Classpath

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

The most common cause is simply that commons-logging.jar (or commons-logging-api.jar) isn’t on the classpath when the application starts. This often happens when developers rely on transitive dependencies without explicitly declaring them in their build tool configuration. Maven or Gradle might resolve the dependency during compilation but fail to package it correctly—especially in WAR or executable JAR deployments.

Classpath Conflicts and Multiple Versions

Enterprise applications sometimes include multiple versions of Commons Logging. Servlet containers like Tomcat or WebLogic ship their own version of the library. When two copies of LogFactory exist at different levels of the classloader hierarchy, the JVM may load the wrong one—or fail to load it entirely. This is particularly tricky in environments using complex modular classloading (e.g., OSGi or Java EE).

How to Resolve the Java.Lang.NoClassDefFoundError: Org/Apache/Commons ...
How to Resolve the Java.Lang.NoClassDefFoundError: Org/Apache/Commons ...

Incorrect Build Configuration

In Maven projects, setting the scope of Commons Logging to provided assumes the runtime environment will supply the JAR. If you deploy to a container without Commons Logging installed, you’ll hit this error immediately. Similarly, shading or excluding the JAR by mistake in an uber-JAR build can strip out essential classes silently.

How to Fix the LogFactory Error

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory源码 ...
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory源码 ...

Add the Commons Logging Dependency Explicitly

The fastest fix is to add Commons Logging directly to your pom.xml or build.gradle. For Maven:

Tomcat无法启动,报错java.lang.NoClassDefFoundError: org/apache/juli/logging ...
Tomcat无法启动,报错java.lang.NoClassDefFoundError: org/apache/juli/logging ...
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory源码 ...
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory源码 ...
Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache ...
Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache ...
Exception in thread main java lang noclassdeffounderror org apache ...
Exception in thread main java lang noclassdeffounderror org apache ...
Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache ...
Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache ...
Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache ...
Exception in thread “main“ java.lang.NoClassDefFoundError: org/apache ...
exception in thread "main" java.lang.NoClassDefFoundError: org/apache ...
exception in thread "main" java.lang.NoClassDefFoundError: org/apache ...
出现“org/apache/commons/logging/LogFactory”的错误解决方法_无法解析 'logfactory' 中的方法 ...
出现“org/apache/commons/logging/LogFactory”的错误解决方法_无法解析 'logfactory' 中的方法 ...
java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory问题 ...
java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory问题 ...
java.lang.NoClassDefFoundError: org/apache/commons/httpclient/protocol ...
java.lang.NoClassDefFoundError: org/apache/commons/httpclient/protocol ...
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决 ...
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决 ...
JAVA运行时类存在,但是报错:NoClassDefFoundError: Could not initialize class-CSDN博客
JAVA运行时类存在,但是报错:NoClassDefFoundError: Could not initialize class-CSDN博客
java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory ...
java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory ...
tomcat启动Java.Lang.NoClassDefFoundError: Org/Apache/Commons/Logging ...
tomcat启动Java.Lang.NoClassDefFoundError: Org/Apache/Commons/Logging ...

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

After adding it, run mvn clean install and verify the JAR appears in your target/ directory or proper deployment module.

Audit Classloader Hierarchies in Application Servers

In Tomcat, place Commons Logging JARs in the lib/ folder, not inside individual WARs, to avoid conflicts. In WebLogic, check the prefer-web-inf-classes setting in weblogic.xml. Always ensure only one version of the JAR exists at the appropriate classloader level.

Switch to a Modern Logging Framework

Consider migrating to commons-logging-api with SLF4J and Logback. Many modern frameworks (including Spring Boot) default to SLF4J, eliminating direct Commons Logging alignment issues. Replacing legacy code that imports org.apache.commons.logging.LogFactory with LoggerFactory.getLogger() provides a long-term, maintainable solution.