5

i'm following this tutorial https://spring.io/guides/gs/rest-service/ to build a restful application. when i run this app (run as spring boot application) from spring tool suite it throwing error :

Caused by: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:155)
        at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
        at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:273)
        at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:190)
        at webapp.Application.main(Application.java:9)
        ... 6 more
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory

same error also happen when i tried to use the project initializr (https://start.spring.io/).

my pom.xml is exact like the tutorial :

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.rondox.sb.restfulws</groupId>
<artifactId>wawa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>wawa</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

my dependency tree :

[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ wawa ---
[INFO] com.rondox.sb.restfulws:wawa:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.5.2.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.5.2.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:1.5.2.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.2.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.2.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.11:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.1.11:compile
[INFO] |  |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.24:compile
[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.24:compile
[INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.24:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.17:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.5.2.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.11:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.11:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.11:compile
[INFO] |  +- org.hibernate:hibernate-validator:jar:5.3.4.Final:compile
[INFO] |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] |  |  \- com.fasterxml:classmate:jar:1.3.3:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.7:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.8.7:compile
[INFO] |  +- org.springframework:spring-web:jar:4.3.7.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-aop:jar:4.3.7.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-beans:jar:4.3.7.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-context:jar:4.3.7.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:4.3.7.RELEASE:compile
[INFO] |     \- org.springframework:spring-expression:jar:4.3.7.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.5.2.RELEASE:test
[INFO] |  +- org.springframework.boot:spring-boot-test:jar:1.5.2.RELEASE:test
[INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:1.5.2.RELEASE:test
[INFO] |  +- junit:junit:jar:4.12:test
[INFO] |  +- org.assertj:assertj-core:jar:2.6.0:test
[INFO] |  +- org.mockito:mockito-core:jar:1.10.19:test
[INFO] |  |  \- org.objenesis:objenesis:jar:2.1:test
[INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] |  +- org.skyscreamer:jsonassert:jar:1.4.0:test
[INFO] |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] |  +- org.springframework:spring-core:jar:4.3.7.RELEASE:compile
[INFO] |  \- org.springframework:spring-test:jar:4.3.7.RELEASE:test
[INFO] \- com.jayway.jsonpath:json-path:jar:2.2.0:test
[INFO]    +- net.minidev:json-smart:jar:2.2.1:test
[INFO]    |  \- net.minidev:accessors-smart:jar:1.1:test
[INFO]    |     \- org.ow2.asm:asm:jar:5.0.3:test
[INFO]    \- org.slf4j:slf4j-api:jar:1.7.24:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.750 s
[INFO] Finished at: 2017-04-09T12:05:22+07:00

have tried this solution ClassNotFoundException: org.slf4j.LoggerFactory to add the latest jar to the classpath but it giving another error :

SLF4J: Class path contains multiple SLF4J bindings.

please throw some enlightment, i have been struggle for this issue for 3 days.

Community
  • 1
  • 1
cumibulat
  • 434
  • 3
  • 6
  • 22

5 Answers5

4

You have more than one logger in your dependencies and slf4j doesn't know which one to use. You should check Maven's dependency tree to fix it.

logback-classic <-> log4j-over-slf4j <-> jboss-logging (?)

You can exclude Spring Boot Logging (Logback by default) and declare Log4j2 manually:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
Guillaume F.
  • 5,905
  • 2
  • 31
  • 59
  • i have add your suggested exclusions but still throwing same error. should i add external jar (slf4j) or just used from the maven dependency ? – cumibulat Apr 09 '17 at 13:56
  • No, you should never add external JAR files. Always rely on Maven to manage the dependancies. Do you have external JARs? If so, this is certainly your issue. Because my suggestion above should fix your problem, unless you have another one hidden somewhere in your code which is not visible in the question. – Guillaume F. Apr 11 '17 at 09:48
  • i have tried both ways. with or without external JAR. yes for all i know this Maven should manage our dependencies and i also can't remove the jar in maven dependency except i add exclusion or change my pom.xml. may i know if you have same issue with me or you can run the apps normally if you just used the spring boot project starter from the initializr ? thanks – cumibulat Apr 11 '17 at 11:36
  • I have pretty large projects with a similar POMs and no conflicts. When you say that you can't remove the JAR, you always can; push it to your Maven repository and use it as a normal dependency. You should check inside of that JAR, if it contains other loggers. – Guillaume F. Apr 11 '17 at 12:24
2

after few times of trial and error, i found out my slf4j jar is corrupted. i can see it when i run mvn package command and there's warning. i renamed the jar, then it will redownload the jar. as soon as the jar not corrupted, then the application face another error. i just followed this source Spring Boot: New Project - UnsatisfiedDependencyException: Error creating bean with name 'methodValidationPostProcessor' to add hibernate validator in exclusion. after that i can run my app normally. thanks all.

Community
  • 1
  • 1
cumibulat
  • 434
  • 3
  • 6
  • 22
2

You may use

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.12</version>
    </dependency>
Enamul Haque
  • 823
  • 1
  • 14
  • 24
0

It was happening with me too, I changed the spring-boot version for a previous version and it works. I changed the 1.5.6-RELEASE to 1.3.5-RELEASE

0

Add missing Logback JARs into the classpath. The JARs can be found in Maven Central.Following are the two jar files

  1. logback-core-1.0.13.jar
  2. logback-classic-1.0.13.jar