ClassNotFoundException: org.apache.commons.logging.LogFactory

Starting a web application, but hits the following error messages :

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

1. Normal Case

1.1 Obviously, the Apache Commons logging is missing – commons-logging-xxx.jar. To fix it, get it from Maven central repository.

pom.xml

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

2. Spring Case

2.1 For Spring application, developers always excluded the commons-logging, but forget to include another logging framework. For example

pom.xml

   <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-webmvc</artifactId>
	  <version>${spring.version}</version>
	  <exclusions>
	      <exclusion>
		  <groupId>commons-logging</groupId>
		  <artifactId>commons-logging</artifactId>
	      </exclusion>
	  </exclusions>
   </dependency>

The above declaration will cause this classNotFoundException: org.apache.commons.logging.LogFactory also.

2.2 To fix it, declares another logging framework, often, this is SLF4j and redirect the Spring’s logging via a bridge.

pom.xml

   <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-webmvc</artifactId>
	  <version>${spring.version}</version>
	  <exclusions>
	      <exclusion>
		  <groupId>commons-logging</groupId>
		  <artifactId>commons-logging</artifactId>
	      </exclusion>
	  </exclusions>
   </dependency>

   <!-- bridge jck to slf4j -->
   <dependency>
	  <groupId>org.slf4j</groupId>
	  <artifactId>jcl-over-slf4j</artifactId>
	  <version>${jcl.slf4j.version}</version>
   </dependency>

   <dependency>
	  <groupId>ch.qos.logback</groupId>
	  <artifactId>logback-classic</artifactId>
	  <version>${logback.version}</version>
   </dependency>

References

  1. Apache Commons Logging
  2. Spring MVC + Logback SLF4j example

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
11 Comment threads
4 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
14 Comment authors
Jason RembertAndreaThenmozhimahalakshmisusi Recent comment authors
newest oldest most voted
Steff
Guest
Steff

add following jars
1. commons-logging 1.x
2.log4j 1.x

Steff
Guest
Steff

you are welcome, Pwale

Osolya
Guest
Osolya

you have saved me!

Thank you very much!

srikanth
Guest
srikanth

Thank you very much mkyong..i l u…

Bhawna
Guest
Bhawna

Thanks a lot 🙂 it worked for me now 🙂

Alvaro
Guest
Alvaro

Thank you! Helped me a lot!

susi
Guest
susi

Hi sir!
Am new to Spring,please kindly help me to get rid of this ERROR!
when ever i try to run a spring project am getting the following errors:
Exception in thread “main” org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException) (Caused by org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException))
at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
at org.springframework.context.support.AbstractApplicationContext.(AbstractApplicationContext.java:158)
at org.springframework.context.support.AbstractRefreshableApplicationContext.(AbstractRefreshableApplicationContext.java:89)
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.(AbstractRefreshableConfigApplicationContext.java:59)
at org.springframework.context.support.AbstractXmlApplicationContext.(AbstractXmlApplicationContext.java:61)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:136)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.byteslounge.spring.tx.Main.main(Main.java:16)
Caused by: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException)
at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
… 10 more
Caused by: java.lang.NullPointerException
at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:374)
… 11 more

please give me a solution for this ERROR.

thanks in advance!

mahalakshmi
Guest
mahalakshmi

Hi MKyong, i am new to spring project,please kindly help me to resolve this error actually in project we are using sts and to do indexing we are using solr. Indexing is running fine at the time of spring 3.2.3 version after updating in to spring 3.2.4 version indexing is not working and i am getting below error ERROR context.ContextLoader: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.scheduling.quartz.SchedulerFactoryBean#1’ defined in ServletContext resource [/WEB-INF/classes/application-context.xml]: Invocation of init method failed; nested exception is org.quartz.JobPersistenceException: The job (DEFAULT.productIndexJobDetail) referenced by the trigger does not exist. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)… Read more »

Thenmozhi
Guest
Thenmozhi

Hi…I am new to eclipse.I dont know how to add esper libraries with eclipse.But i copied a program from net and tried running it.I got noclass def error.So i downloaded logging jar file and added.Then also i am getting same error.Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at com.espertech.esper.client.Configuration.(Configuration.java:39)
at com.espertech.example.main(example.java:53)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(Unknown Source)
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)
… 2 more
This is the error i got.Could you please help me with a simple codding.I am a beginner.so please give it as concise as possible.

Andrea
Guest
Andrea

Great!!!!!

Jason Rembert
Guest
Jason Rembert

Hello,

you can download the JAR with dependencies without installing Maven on: http://jar-download.com/