Explain Codes LogoExplain Codes Logo

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

java
class-not-found
logging
classpath
Alex KataevbyAlex Kataev·Feb 8, 2025
TLDR

To tackle the ClassNotFoundException for org.apache.commons.logging.LogFactory, add the Apache Commons Logging JAR to your project. Plug this into your Maven pom.xml:

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

Or, if you're #TeamGradle, add this to build.gradle:

// Sending the rescue squad! ಠ‿↼ implementation 'commons-logging:commons-logging:1.2'

This quick fix should help your application find the required logging classes like a treasure hunter finds gold!

Locating our JAR on the classpath

Ensure the commons-logging.jar is present in your classpath, like making sure you haven't left your wallet at home:

  1. Eclipse users, navigate through Project PropertiesJava Build Path.
  2. IntelliJ aficionados, go to FileProject StructureLibraries.

Old school and not using an IDE or build tools like Maven or Gradle? Download commons-logging.jar from the Apache website and sneak it into your application's lib folder.

Exploring other cool logging frameworks

Fancy some SLF4J action instead? Replace commons-logging with SLF4J's bridging module jcl-over-slf4j:

<dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.30</version> </dependency>

This reroutes calls to admin's favourite, commons-logging, to SLF4J, for advanced logging management. It's like taking a detour but ending up at a better destination!

Fine-tuning and compatibility checks

Ensure no typos have crept into the pom.xml or build.gradle. Recheck the groupIds and artifactIds for precision. You wouldn't mistype your own name, right?

While version 1.2 is stated here, different projects may go better with 1.1.1 or 1.1.2. Always opt for the version that clicks with your project setup best.

Persisting issues and resolution

  • Spot the exact name of the LogFactory class in the JAR; a decompiler plugin might be of immense help here.
  • Juggling multiple projects? Check for clashing versions of commons-logging on the classpath.
  • When you're rolling out your app, confirm the JAR is part of the deployment artifact (WAR, EAR, or a fat JAR).