Thursday, September 28, 2023

How to fix java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory

java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory or "Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory" exception comes when you don't apache commons-logging-1.1.1.jar in your Classpath. I have faced this exception many times while using open-source frameworks like Struts, Spring, and Displaytag which uses a commons-logging framework for logging. commons-logging is not an actual java logging framework but provides a wrapper so that you can use log4j, java logging, or any other Java logging framework.

The commons-logging is also smart enough to detect which logging framework are you using e.g if you are using log4j it can detect it by looking log4j.jar in your classpath.

Btw if you don't have commons-logging.jar in your classpath you may get java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory at runtime even if your application gets deployed successfully.


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

java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory ErrorReal cause of "Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory" or "java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory" is JVM not able to locate commons-logging-1.1.1.jar in classpath but it may surface differently on different environment. Some time even you get this error even if you already have commons-logging-1.1.1.jar in classpath and many times it different reasons for different environment. 

It gets more difficult on the managed environments like J2EE web servers and application servers where different classloaders load different classes e.g. AppClassLoader is responsible for loading web application but classes inside lib directory of web server e.g. tomcat are loaded by another classloader. 

So you can also get "java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory" if commons-logging.jar is not visible to App classloader. some times this jar can also cause a memory leak in tomcat.



here I have outlined some steps to fix "java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory" these are based on the approach I used to resolve such kind of error. it may help you as well.

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

1.Check your classpath for commons-logging-1.1.1.jar, if you are using spring MVC and working in a web application it must be on /WEB-INF/lib directory.

2.Check if your Java program has sufficient permission to access that jar file (I faced this issue in Unix machine when a user who copied that jar does override the default permission and the application was not able to read this jar file so make sure to check permissions if you are copying commons-logging-1.1.1.jar into Java Classpath.

3.See if commons-logging-1.1.1.jar contains org.apache.commons.logging.LogFactory class or not this may happen if commons-logging-1.1.1.jar is corrupt or not downloaded completely or may be downloaded from an incorrect source.

4.See if your Classpath is getting overridden by any start-up Script as suggested in my post on how to solve java.lang.ClassNotFoundException

5.If you are using maven for managing dependency on your project then check if your pom.xml file contains the following entry or not and if it doesn't then include the following dependency for commons-logging-1.1.1.jar in pom.xml:
Maven dependency for Apache commons-logging:

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


That's all on How to fix java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory Error, it may surface as "Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory" also but mostly cause of both errors are same. 

If you have any other useful step to figure out ClassNotFoundException suggest it here.


Here are few more Java tutorials you may like.

And lastly one question for you? What is your approach to solve any exception in Java? Do you get panicked? or do you start checking log files then stack trace and then try to make sense of it? 

9 comments :

Amit Bhargava said...

Not quite a question on this article, but here goes.

I've got a common.jar file that I build using maven and store in my local repository so that i can build other modules. Is there any way I can delete previous versions of the jar while storing it in repository?

Hugo Prudente said...

Tks Brow.
I use this solution, to check my commons-loggin*.jar. They didn't go to web-inf/lib folder.

So i delete my target folder from my javaee project and clean/build again my solution, i restart my server and make the new deploy and works.

Tks.

Ankur said...

Hi , I have query ::

I am looking to fix an java.lang.ClassNotFoundException: com.reportmill.shape.RMShape .

I am unable to find the solution.

Plz help me>:)

Anonymous said...

I a getting Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.springframework.context.support.AbstractApplicationContext.(AbstractApplicationContext.java:158)
at org.springframework.context.support.GenericApplicationContext.(GenericApplicationContext.java:102)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext.java:60)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext.java:82)
at hello.Application.main(Application.java:21)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown 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

while working with spring 4.2.1 version and annotation drive application context. which JAR should I add into claspath?

Anonymous said...

Hi i am getting this error in tesng "Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/IOUtils" .

Is there any way to solve this ? Please advice

Sukron Samosir said...

try to add commons-cli-1.2.jar library. It's work for me

Sukron Samosir said...

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/MissingArgumentException
at org.jpos.util.Logger.log(Logger.java:79)
at org.jpos.iso.BaseChannel.connect(BaseChannel.java:409)
at pbbclient.EchoTest.main(EchoTest.java:35)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.MissingArgumentException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more


This is my problem before

javin paul said...

As Sukron suggested try adding commons-cli-1.2.jar and it would solve the problem.

Anonymous said...

springboot version 2.6 or 2.7 jdk 11 ,log4jVersion = '2.17.0'
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)
Caused by: java.lang.NoClassDefFoundError: org/apache/logging/log4j/message/SimpleMessage
at org.apache.logging.log4j.message.AbstractMessageFactory.newMessage(AbstractMessageFactory.java:59)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2017)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1983)
at org.apache.commons.logging.LogAdapter$Log4jLog.log(LogAdapter.java:263)
at org.apache.commons.logging.LogAdapter$Log4jLog.error(LogAdapter.java:215)
at org.springframework.boot.SpringApplication.reportFailure(SpringApplication.java:824)
at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:788)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:318)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
at com.intel.security.datapipeline.Application.main(Application.java:65)
... 8 more
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.message.SimpleMessage
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:476)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 19 more
Exception in thread "SpringApplicationShutdownHook" java.lang.NoClassDefFoundError: org/springframework/boot/availability/ReadinessState
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.doClose(ServletWebServerApplicationContext.java:172)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:1021)
at org.springframework.boot.SpringApplicationShutdownHook.closeAndWait(SpringApplicationShutdownHook.java:145)
at java.base/java.lang.Iterable.forEach(Iterable.java:75)
at org.springframework.boot.SpringApplicationShutdownHook.run(SpringApplicationShutdownHook.java:114)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.availability.ReadinessState
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:476)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 6 more

Post a Comment