"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.
What Is the NoClassDefFoundError?

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.

Why LogFactory Disappears at Runtime
Missing JAR in the Classpath

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).

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
Add the Commons Logging Dependency Explicitly
The fastest fix is to add Commons Logging directly to your pom.xml or build.gradle. For Maven:











<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.