196

i am executing simple Dependency Injection program of spring & getting this exception. I have already included common-logging1.1.1.jar and spring.jar file. Could you please help to out?

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:119)
    at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:55)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:65)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:56)
    at com.client.StoryReader.main(StoryReader.java:15)
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)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    ... 6 more
skaffman
  • 381,978
  • 94
  • 789
  • 754
  • 2
    First, double check that your classpath is indeed correct. Second, try opening the jar and check that it actually contains the org.apache.commons.logging.LogFactory class. Finally, try debugging by setting a breakpoint in the class loader. – Kolibri Oct 11 '09 at 22:06
  • 2
    All the answers below suggesting ```commons-logging``` is a step backwards. Use ```jcl-over-slf4j``` instead. – opyate Feb 25 '15 at 16:05
  • In Android add testCompile 'commons-logging:commons-logging:1.1.1' to app.gradle – Roger Garzon Nieto Sep 02 '17 at 12:44

25 Answers25

122

If you're using maven for managing dependencies, add the following line in your pom.xml:

<dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
</dependency>
asuka
  • 1,879
  • 3
  • 17
  • 23
120

I have also faced the same issues, to fix, download the jar files from the below url

http://commons.apache.org/logging/download_logging.cgi

and copy to your lib folder, will resolve your issue.

J. Steen
  • 14,900
  • 15
  • 57
  • 62
Manikandan
  • 1,233
  • 1
  • 8
  • 3
41

You just download commons-logging-1.1.2.jar and then copy this file in to libs

finally, it works.

sonida
  • 4,321
  • 1
  • 36
  • 39
20

commons-logging-1.1.1.jar or jcl-over-slf4j-1.7.6.jar al

If you are using maven, use the below code.

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>${slf4j.version}</version>
</dependency>
Paramesh Korrakuti
  • 1,583
  • 3
  • 21
  • 30
18

I had the same problem, and solved it by just adding the commons-logging.jar to the class path.

Jesse
  • 8,035
  • 7
  • 42
  • 56
stacky
  • 181
  • 1
  • 4
8

Adding commons-logging.jar or commons-logging-1.1.jar will solve this...

APal
  • 81
  • 1
  • 2
7

Setting the scope to compile did it for me

<dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  <version>1.2</version>
  <scope>compile</scope>
</dependency>
  • 1
    Solved my situation. When running Mockito test cases, I got this error and having this dependency in pom.xml solved. – venugopal Jun 30 '17 at 20:38
6

I have already included common-logging1.1.1.jar and ...

Are you sure you spelled the name of the JAR file exactly right? I think it should probably be commons-logging-1.1.1.jar (note the extra - in the name). Also check if the directory name is correct.

NoClassDefFoundError always means that a class cannot be found, so most likely your class path is not correct.

Jesper
  • 186,095
  • 42
  • 296
  • 332
  • 2
    `NoClassDefFoundError` can also be caused by other corner cases, like when class initializers fail in obscure circumstances. – skaffman Oct 11 '09 at 21:12
  • Furthermore, if he gets a NoClassDefFoundError because the class instantiation failed, then he should have gotten a ExceptionInInitializerError exception first (unless of course it was caught silently). – Kolibri Oct 11 '09 at 22:04
  • And don't forget the "s" `commons-logging-1.1.1.jar` – Glenn Lawrence Mar 16 '15 at 00:21
  • @GlennLawrence Thanks, fixed the name. – Jesper Mar 16 '15 at 07:39
4

Try doing a complete clean of the target/deployment directory for the app to get rid of any stale library jars. Make a fresh build and check that commons-logging.jar is actually being placed in the correct lib folder. It might not be included when you are building the library for the application.

emills
  • 501
  • 2
  • 8
4

Issue solved by adding commons-logging.jar

Imp files are ,

antlr-runtime-3.0.1

org.springframework.aop-3.1.0.M2

org.springframework.asm-3.1.0.M2

org.springframework.aspects-3.1.0.M2

org.springframework.beans-3.1.0.M2

org.springframework.context.support-3.1.0.M2

org.springframework.context-3.1.0.M2

org.springframework.core-3.1.0.M2

org.springframework.expression-3.1.0.M2

commons-logging-1.1.1
Dhanuka
  • 2,704
  • 5
  • 25
  • 37
Tush
  • 161
  • 1
  • 9
3

Two options (at least):

  1. Add the commons-logging jar to your file by copying it into a local folder.

Note: linking the jar can lead to problems with the server and maybe the reason why it's added to the build path but not solving the server startup problem.

So don't point the jar to an external folder.

OR...

  1. If you really don't want to add it locally because you're sharing the jar between projects, then...

If you're using a tc server instance, then you need to add the jar as an external jar to the server instance run configurations.

go to run as, run configurations..., {your tc server instance}, and then the Class Path tab.

Then add the commons-logging jar.

devaaron
  • 101
  • 8
3

I got the same trouble than you. Finally I checked the version of apache possessing the class. I found that the version 1.0.4 has the class.

Try to use the version 1.0.4 instead of 1.1.X or 1.2.X

My dependencies :

    <dependencies>
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
            <version>1.3.5</version>
        </dependency>
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-client-java</artifactId>
            <version>1.3.5</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.0.4</version>
        </dependency>
    </dependencies>

My Java Code

J4pClient j4pClient = new J4pClient("http://localhost:8080/jolokia");
J4pReadRequest req = new J4pReadRequest("java.lang:type=Memory","HeapMemoryUsage");
req.setPath("used");
J4pReadResponse resp = j4pClient.execute(req);
System.out.println(resp.getValue());

My Result :

130489168

Double check also that your maven dependencies are well imported.

歌永_
  • 71
  • 1
1

http://commons.apache.org/logging/download_logging.cgi

use this url to download jar files and include them in your class path, issue will be solved

rash
  • 19
  • 1
0

Just check whether the commons-logging.jar has been added to your libs and the classpath.. I had the same issue and that was because of this. dhammikas-

0

I generally assign the classpath to a variable and then verify it. I've written a small ruby script which I include in a my startup scripts which validates the classpath before launching java. Validating the classpath before the JVM starts has saved me lots of time troubleshooting these types of problems.

brianegge
  • 26,898
  • 12
  • 70
  • 97
0

Hey I was following the tutorial on tutorialpoint.com. Add after you complete Step 2 - Install Apache Common Logging API: You must import external jar libraries to the project from the files downloaded at this step. For me the file name was "commons-logging-1.1.1".

The Third
  • 637
  • 1
  • 7
  • 25
0

If you're running this on Android then note that apparently java.beans package is not complete on Android. To attempt to fix it on Android try the following:

  1. Download android-java-air-bridge.jar (currently the download button is on the bottom of the page or direct link here)
  2. Copy the downloaded jar to your [APPROOT]/app/libs directory (or link the jar in any other way)
  3. Change the import *** statements to that of air-bridge. Eg import javadz.beanutils.BeanUtils instead of import org.apache.commons.beanutils.BeanUtils;
  4. Clean and rebuild the project

source 1, source 2

I apologise as I realise this is not exactly answering the question, though this SO page comes up a lot when searching for android-generated NoClassDefFoundError: Failed resolution of: beanUtils errors.

Community
  • 1
  • 1
Voy
  • 2,938
  • 1
  • 30
  • 43
0

I was getting the same error while the jar was present. No solution worked. What worked was deleting the jar from the file system (from .m2 directory) and then cleaning the maven project.

Anmol Gupta
  • 2,299
  • 5
  • 23
  • 38
0

I have the same problem in eclipse IDE, my solution was: Right click in My project > Properties

enter image description here

Click in Maven and write: jar in the Active Maven Project

enter image description here

Finally, Apply and Close

Yuliem Alavez
  • 53
  • 1
  • 5
0

In my case I was testing a Tomcat app in eclipse and got this error. I solved it by checking the .classpath file and corrected this entry:

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
    <attributes>
        <attribute name="maven.pomderived" value="true"/>
        <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
    </attributes>
</classpathentry>

The attribute org.eclipse.jst.component.dependency had been missing.

paul
  • 12,710
  • 21
  • 76
  • 136
-1

Check whether the jars are imported properly. I imported them using build path. But it didn't recognise the jar in WAR/lib folder. Later, I copied the same jar to war/lib folder. It works fine now. You can refresh / clean your project.

-2

Hello friends if your getting any not class found exception in hibernate code it is the problem of jar files.here mainly two problems
1.I mean to say your working old version of hibernate may be 3.2 bellow.So if u try above 3.6 it will works fine

2.first checkes database connection.if it database working properly their was a mistake in ur program or jar file.

please check these two prioblems if it also not working you tried to IDE . I am using netbeanside 6.9 version.here hibernate working fine.you dont get any error from class not founnd exception..

I hope this one helps more

-2

try adding this dependency org.apache.commons commons-exec 1.3

user1244663
  • 27
  • 1
  • 2
-3

If all else fails, as it had for me, try putting the commons-logging-x.y.z.jar in your Tomcat lib directory. It solved the problem! BTW, I am using Tomcat 6.

Bob V.
  • 1
-4

Solution is to Add common-logging.x.x jar file

Sachin
  • 59
  • 3