Class path contains multiple SLF4J bindings error
Asked Answered
S

5

22

I get this error when tring to run gradle build

i understand it a versions conflict but not sure how to solve it and which

version to exclude...

gradle dependency tree is:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/i/.gradle/caches/modules-
2/files-2.1/org.slf4j/slf4j- 
log4j12/1.6.1/bd245d6746cdd4e6203e976e21d597a46f115802/slf4j-log4j12-
1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in   
[jar:file:/C:/Users/i/.gradle/caches/modules-2/files-
2.1/ch.qos.logback/logback- 
classic/1.1.3/d90276fff414f06cb375f2057f6778cd63c6082f/logback-classic-
1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an    
explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
SLF4J: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class    
path, preempting StackOverflowError. 


 compile - Compile classpath for source set 'main'.
 +--- com.google.code.gson:gson:2.2.4
 +--- com.fasterxml.jackson.core:jackson-core:2.6.0
 +--- com.fasterxml.jackson.core:jackson-databind:2.6.0
 |    +--- com.fasterxml.jackson.core:jackson-annotations:2.6.0
 |    \--- com.fasterxml.jackson.core:jackson-core:2.6.0
 +--- com.fasterxml.jackson.core:jackson-annotations:2.6.0
 +--- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.6.0
 |    +--- com.fasterxml.jackson.core:jackson-core:2.6.0
 |    \--- org.yaml:snakeyaml:1.15
 +--- org.projectlombok:lombok:1.16.10
 +--- javax.validation:validation-api:1.1.0.Final
 +--- org.apache.commons:commons-lang3:3.4
 +--- org.apache.zookeeper:zookeeper:3.4.5
 |    +--- org.slf4j:slf4j-api:1.6.1 -> 1.7.21
 |    +--- org.slf4j:slf4j-log4j12:1.6.1
 |    |    +--- org.slf4j:slf4j-api:1.6.1 -> 1.7.21
 |    |    \--- log4j:log4j:1.2.16
 |    +--- log4j:log4j:1.2.15 -> 1.2.16
 |    +--- jline:jline:0.9.94
 |    |    \--- junit:junit:3.8.1
 |    \--- org.jboss.netty:netty:3.2.2.Final
 +--- org.apache.httpcomponents:httpcore:4.4.5 -> 4.4.6
 +--- org.apache.httpcomponents:httpclient:4.5.3
 |    +--- org.apache.httpcomponents:httpcore:4.4.6
 |    +--- commons-logging:commons-logging:1.2
 |    \--- commons-codec:commons-codec:1.9
 +--- commons-io:commons-io:2.5
 +--- org.jdom:jdom2:2.0.5
 \--- org.slf4j:slf4j-api:1.7.21

 default - Configuration for default artifacts.
 +--- com.google.code.gson:gson:2.2.4
 +--- com.fasterxml.jackson.core:jackson-core:2.6.0
 +--- com.fasterxml.jackson.core:jackson-databind:2.6.0
 |    +--- com.fasterxml.jackson.core:jackson-annotations:2.6.0
 |    \--- com.fasterxml.jackson.core:jackson-core:2.6.0
 +--- com.fasterxml.jackson.core:jackson-annotations:2.6.0
 +--- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.6.0
 |    +--- com.fasterxml.jackson.core:jackson-core:2.6.0
 |    \--- org.yaml:snakeyaml:1.15
 +--- org.projectlombok:lombok:1.16.10
 +--- javax.validation:validation-api:1.1.0.Final
 +--- org.apache.commons:commons-lang3:3.4
 +--- org.apache.zookeeper:zookeeper:3.4.5
 |    +--- org.slf4j:slf4j-api:1.6.1 -> 1.7.21
 |    +--- org.slf4j:slf4j-log4j12:1.6.1
 |    |    +--- org.slf4j:slf4j-api:1.6.1 -> 1.7.21
 |    |    \--- log4j:log4j:1.2.16
 |    +--- log4j:log4j:1.2.15 -> 1.2.16
 |    +--- jline:jline:0.9.94
 |    |    \--- junit:junit:3.8.1
 |    \--- org.jboss.netty:netty:3.2.2.Final
 +--- org.apache.httpcomponents:httpcore:4.4.5 -> 4.4.6
 +--- org.apache.httpcomponents:httpclient:4.5.3
 |    +--- org.apache.httpcomponents:httpcore:4.4.6
 |    +--- commons-logging:commons-logging:1.2
 |    \--- commons-codec:commons-codec:1.9
 +--- commons-io:commons-io:2.5
 +--- org.jdom:jdom2:2.0.5
 \--- org.slf4j:slf4j-api:1.7.21

 runtime - Runtime classpath for source set 'main'.
 +--- com.google.code.gson:gson:2.2.4
 +--- com.fasterxml.jackson.core:jackson-core:2.6.0
 +--- com.fasterxml.jackson.core:jackson-databind:2.6.0
 |    +--- com.fasterxml.jackson.core:jackson-annotations:2.6.0
 |    \--- com.fasterxml.jackson.core:jackson-core:2.6.0
 +--- com.fasterxml.jackson.core:jackson-annotations:2.6.0
 +--- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.6.0
 |    +--- com.fasterxml.jackson.core:jackson-core:2.6.0
 |    \--- org.yaml:snakeyaml:1.15
 +--- org.projectlombok:lombok:1.16.10
 +--- javax.validation:validation-api:1.1.0.Final
 +--- org.apache.commons:commons-lang3:3.4
 +--- org.apache.zookeeper:zookeeper:3.4.5
 |    +--- org.slf4j:slf4j-api:1.6.1 -> 1.7.21
 |    +--- org.slf4j:slf4j-log4j12:1.6.1
 |    |    +--- org.slf4j:slf4j-api:1.6.1 -> 1.7.21
 |    |    \--- log4j:log4j:1.2.16
 |    +--- log4j:log4j:1.2.15 -> 1.2.16
 |    +--- jline:jline:0.9.94
 |    |    \--- junit:junit:3.8.1
 |    \--- org.jboss.netty:netty:3.2.2.Final
 +--- org.apache.httpcomponents:httpcore:4.4.5 -> 4.4.6
 +--- org.apache.httpcomponents:httpclient:4.5.3
 |    +--- org.apache.httpcomponents:httpcore:4.4.6
 |    +--- commons-logging:commons-logging:1.2
 |    \--- commons-codec:commons-codec:1.9
 +--- commons-io:commons-io:2.5
 +--- org.jdom:jdom2:2.0.5
 \--- org.slf4j:slf4j-api:1.7.21

 testCompile - Compile classpath for source set 'test'.
 +--- com.google.code.gson:gson:2.2.4
 +--- com.fasterxml.jackson.core:jackson-core:2.6.0
 +--- com.fasterxml.jackson.core:jackson-databind:2.6.0
 |    +--- com.fasterxml.jackson.core:jackson-annotations:2.6.0
 |    \--- com.fasterxml.jackson.core:jackson-core:2.6.0
 +--- com.fasterxml.jackson.core:jackson-annotations:2.6.0
 +--- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.6.0
 |    +--- com.fasterxml.jackson.core:jackson-core:2.6.0
 |    \--- org.yaml:snakeyaml:1.15
 +--- org.projectlombok:lombok:1.16.10
 +--- javax.validation:validation-api:1.1.0.Final
 +--- org.apache.commons:commons-lang3:3.4
 +--- org.apache.zookeeper:zookeeper:3.4.5
 |    +--- org.slf4j:slf4j-api:1.6.1 -> 1.7.21
 |    +--- org.slf4j:slf4j-log4j12:1.6.1
 |    |    +--- org.slf4j:slf4j-api:1.6.1 -> 1.7.21
 |    |    \--- log4j:log4j:1.2.16
 |    +--- log4j:log4j:1.2.15 -> 1.2.16
 |    +--- jline:jline:0.9.94
 |    |    \--- junit:junit:3.8.1 -> 4.11
 |    |         \--- org.hamcrest:hamcrest-core:1.3
 |    \--- org.jboss.netty:netty:3.2.2.Final
 +--- org.apache.httpcomponents:httpcore:4.4.5 -> 4.4.6
 +--- org.apache.httpcomponents:httpclient:4.5.3
 |    +--- org.apache.httpcomponents:httpcore:4.4.6
 |    +--- commons-logging:commons-logging:1.2
 |    \--- commons-codec:commons-codec:1.9
 +--- commons-io:commons-io:2.5
 +--- org.jdom:jdom2:2.0.5
 +--- org.slf4j:slf4j-api:1.7.21
 \--- junit:junit:4.11 (*)

 testRuntime - Runtime classpath for source set 'test'.
 +--- com.google.code.gson:gson:2.2.4
 +--- com.fasterxml.jackson.core:jackson-core:2.6.0
 +--- com.fasterxml.jackson.core:jackson-databind:2.6.0
 |    +--- com.fasterxml.jackson.core:jackson-annotations:2.6.0
 |    \--- com.fasterxml.jackson.core:jackson-core:2.6.0
 +--- com.fasterxml.jackson.core:jackson-annotations:2.6.0
 +--- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.6.0
 |    +--- com.fasterxml.jackson.core:jackson-core:2.6.0
 |    \--- org.yaml:snakeyaml:1.15
 +--- org.projectlombok:lombok:1.16.10
 +--- javax.validation:validation-api:1.1.0.Final
 +--- org.apache.commons:commons-lang3:3.4
 +--- org.apache.zookeeper:zookeeper:3.4.5
 |    +--- org.slf4j:slf4j-api:1.6.1 -> 1.7.21
 |    +--- org.slf4j:slf4j-log4j12:1.6.1
 |    |    +--- org.slf4j:slf4j-api:1.6.1 -> 1.7.21
 |    |    \--- log4j:log4j:1.2.16
 |    +--- log4j:log4j:1.2.15 -> 1.2.16
 |    +--- jline:jline:0.9.94
 |    |    \--- junit:junit:3.8.1 -> 4.11
 |    |         \--- org.hamcrest:hamcrest-core:1.3
 |    \--- org.jboss.netty:netty:3.2.2.Final
 +--- org.apache.httpcomponents:httpcore:4.4.5 -> 4.4.6
 +--- org.apache.httpcomponents:httpclient:4.5.3
 |    +--- org.apache.httpcomponents:httpcore:4.4.6
 |    +--- commons-logging:commons-logging:1.2
 |    \--- commons-codec:commons-codec:1.9
 +--- commons-io:commons-io:2.5
 +--- org.jdom:jdom2:2.0.5
 +--- org.slf4j:slf4j-api:1.7.21
 \--- junit:junit:4.11 (*)

but i dont even know how to read it...

can you help please ?

Scythia answered 20/2, 2017 at 15:44 Comment(1)
As the problem is when running gradle, it cannot be found in your application dependencies. Please show your build script(s).Procarp
C
22

First of all, SLF4J is a simple facade for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time.

What does it mean in simple words?

When you use SLF4J, your code will depend on one common logging interface provided by SLF4J (SLF4J-API), and on the other side you will plug the logging framework of your choice to SLF4J which will allow you to switch different logging frameworks easily.

So, all the logging calls you use in your code while using the SLF4J facade will be delegated to the underlying logging framework.

From the illustration you can see that your app use one common API interface SLF4J API with different log frameworks each time.

enter image description here

Now, let's look on your Gradle output:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/i/.gradle/caches/modules-
2/files-2.1/org.slf4j/slf4j- 
log4j12/1.6.1/bd245d6746cdd4e6203e976e21d597a46f115802/slf4j-log4j12-
1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in   
[jar:file:/C:/Users/i/.gradle/caches/modules-2/files-
2.1/ch.qos.logback/logback- 
classic/1.1.3/d90276fff414f06cb375f2057f6778cd63c6082f/logback-classic-
1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an    
explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
SLF4J: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class    
path, preempting StackOverflowError. 

It's a situation of two different SLF4J logging framework bindings, the first on is org.slf4j/slf4j-log4j12 which is LOG4J logging framework binding, and the second one is ch.qos.logback/logback-classic which is the Logback logging framework, the native implementation of SLFJ API.

So, in order to solve your issue you need to exclude one of them from your classpath.

Specifically here, the problematic artifact is org.apache.zookeeper:zookeeper:3.4.5 so let's exclude one of the logging frameworks:

dependencies {
    compile('org.apache.zookeeper:zookeeper:3.4.5') {
        exclude group: 'ch.qos.logback', module: 'logback-classic'
    }
    //...
}

Reference: SLF4J manual

Culpa answered 16/3, 2017 at 12:54 Comment(6)
How do you exclude it?Spithead
Added an example of exclusion.Culpa
Really thanks for the effort done in elaboration the problem itself and explaining what's slf4j and how it works!Visigoth
@Visigoth happy to help :)Culpa
How did you determine that org.apache.zookeeper:zookeeper:3.4.5 was the problematic artifact?Mariammarian
@Mariammarian it's the lib the OP question that has the slf4j lib included.Culpa
H
5

As @Vampire said, this looks like an issue with the buildscript classpath rather than the compile or runtime configurations. It could probably be fixed via

buildscript {
    configurations {
        classpath {
            exclude group: 'ch.qos.logback', module: 'logback-classic'
            // OR
            exclude group: 'org.slf4j', module: 'slf4j-log4j12'
        }
    }
    dependencies { ... }
} 
Hamlin answered 21/2, 2017 at 11:3 Comment(0)
B
1

Your dependency zoo also contains the org.slf4j:slf4j-api dependency. So just exclude it

+--- org.apache.zookeeper:zookeeper:3.4.5
 |    +--- org.slf4j:slf4j-api:1.6.1 -> 1.7.21
 |    +--- org.slf4j:slf4j-log4j12:1.6.1
 |    |    +--- org.slf4j:slf4j-api:1.6.1 -> 1.7.21    # <-- dependency includes slf4j-api
 |    |    \--- log4j:log4j:1.2.16
 |    +--- log4j:log4j:1.2.15 -> 1.2.16
 |    +--- jline:jline:0.9.94
 |    |    \--- junit:junit:3.8.1
 |    \--- org.jboss.netty:netty:3.2.2.Final
 +--- org.apache.httpcomponents:httpcore:4.4.5 -> 4.4.6
 +--- org.apache.httpcomponents:httpclient:4.5.3
 |    +--- org.apache.httpcomponents:httpcore:4.4.6
 |    +--- commons-logging:commons-logging:1.2
 |    \--- commons-codec:commons-codec:1.9
 +--- commons-io:commons-io:2.5
 +--- org.jdom:jdom2:2.0.5
 +--- org.slf4j:slf4j-api:1.7.21  # <-- Also included as a first tier dependency

In your build.gradle just exclude it from the zookeeper lib.

dependencies {
    compile('org.apache.zookeeper:zookeeper:3.4.5') {
        // exclude transitive logging dependency from zookeeper
        exclude group: 'org.slf4j'
        // might need to exclude log4j, not sure?
        exclude group: 'log4j', module: 'log4j'
    }
    // .. other dependencies
}
Banner answered 20/2, 2017 at 17:22 Comment(1)
This answer is totally wrong. The productive application does not have any problem. Having the slf4j-api in the classpath is correct and is even pulled in transitively by the slf4j bindings. As the error messages states, the problems are (a) that the log4j binding and the logback binding are both in the classpath and (b) that the log4j binding and the log4j bridge are both in the classpath. But as this happens during the build and not during using the final product, this is a matter of buildscript classpath. Your answer is totally wrong and without the buildscript this is not answerable.Procarp
P
0

After trying a couple of things, the answer to the question in the link worked for me.

Class path contains multiple SLF4J bindings with Gradle

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.name == 'log4j') {
            details.useTarget 'log4j:log4j:1.2.+'
        }
    }
}
Pagan answered 9/5, 2020 at 18:59 Comment(0)
T
0

I also got the same error and It was fixed by adding below dependencies. For that I referred this. https://www.slf4j.org/codes.html#multiple_bindings

   <dependency>
    <groupId> org.apache.cassandra</groupId>
    <artifactId>cassandra-all</artifactId>
    <version>0.8.1</version>

    <exclusions>
      <exclusion> 
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
      </exclusion>
      <exclusion> 
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
      </exclusion>
    </exclusions> 

   </dependency>
Tramline answered 26/7 at 2:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.