0

I'm using spring 4.0 and I've started with the hello world example. When I ran the examples, I just got this exception,

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:153)
    at org.springframework.context.support.GenericApplicationContext.<init>(GenericApplicationContext.java:100)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:60)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:82)
    at hello.service.main.Main.main(Main.java:14)
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)
    ... 5 more

But I read that if we are not using commons logging and I want to use slf4j then I just want to add slf4j logging jar in the classpath. I've added that also.

Please help me in resolving the issue.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
vvekselva
  • 803
  • 3
  • 17
  • 34

3 Answers3

1

To use slf4j your pom.xml must include following set of dependencies

     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.0.RELEASE</version>
        <scope>runtime</scope>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.5.8</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.5.8</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.8</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
        <scope>runtime</scope>
    </dependency>
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

download commons-logging.jar and add it to your class-path.

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

download the zip file commons-logging-1.1.3-bin.zip and extract commons-logging.jar, add it to classpath, will solve the issue.

Raghu
  • 1,363
  • 1
  • 23
  • 37
  • But in spring documentation it is mentioned that commons logging can also be avoided http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#overview-logging-slf4j – vvekselva Dec 24 '13 at 09:15
0

Just add the jcl-over-slf4j.jar file to your libraries. See slf4j docu for the details.

Henry
  • 42,982
  • 7
  • 68
  • 84