当前位置: 首页 > 知识库问答 >
问题:

类路径包含多个SLF4J绑定错误

郜谦
2023-03-14

当尝试运行gradle构建时,我得到了这个错误

我理解这是一个版本冲突,但不确定如何解决,以及哪个版本

要排除的版本。。。

gradle依赖关系树是:

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 (*)

但我甚至不知道怎么读…

你能帮忙吗?

共有3个答案

索梓
2023-03-14

您的依赖动物园还包含org.slf4j: slf4j-api依赖项。所以只要排除它

+--- 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

在你的 build.gradle 中,只需将其从动物园管理员库中排除即可。

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
}
罗波鸿
2023-03-14

正如@Vampire所说,这看起来像是buildscript类路径的问题,而不是编译运行时配置的问题。它可能可以通过

buildscript {
    configurations {
        classpath {
            exclude group: 'ch.qos.logback', module: 'logback-classic'
            // OR
            exclude group: 'org.slf4j', module: 'slf4j-log4j12'
        }
    }
    dependencies { ... }
} 
曹浩淼
2023-03-14

首先,SLF4J是各种日志框架(例如java.util.logging、logback、log4j)的简单外观,允许最终用户在部署时插入所需的日志框架。

简单来说,这意味着什么?

当您使用SLF4J时,您的代码将依赖于SLF4J(SLF4J-API)提供的一个通用日志记录接口,另一方面,您将把您选择的日志记录框架插入SLF4J,这将允许您轻松切换不同的日志记录框架。

因此,您在使用SLF4J外观时在代码中使用的所有日志调用都将委托给底层日志框架。

从图中可以看出,您的应用程序使用一个通用的API接口<code>SLF4J API

现在,让我们看看你的Gradle输出:

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. 

这是两个不同的SLF4J日志框架绑定的情况,第一个是org.slf4j/slf4j-log4j12,这是log4j日志框架绑定,第二个是ch.qos.logback/logback-classic,这是Logback日志框架,SLFJ API的本机实现。

因此,为了解决您的问题,您需要从您的类路径中排除其中一个。

具体来说,这里有问题的工件是组织.apache.动物园管理员:动物园管理员:3.4.5,所以让我们排除其中一个日志记录框架:

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

参考:SLF4J说明书

 类似资料:
  • 我得到以下错误。似乎有多个日志记录框架绑定到SLF4J。不知道该怎么解决。非常感谢任何帮助。

  • 我的应用程序服务器ibm WebSphere。我在应用程序服务器日志中得到以下错误。哪里可以设置websphere? [19.09.2012 14:56:54:940 EEST]0000000a SystemErr R SLF4J:类路径包含多个SLF4J绑定。 [19.09.2012 14:56:54:940 EEST]0000000a SystemErr R SLF4J:在[wsjar:fil

  • 我在运行java代码时遇到以下运行时异常。有人能帮我解决绑定冲突吗。

  • 我在netbeans中有一个纯Java项目,我得到的警告是: 当然,我搜索了警告,但所有的答案都与用maven或pom.xml文件删除重复绑定有关。但是我没有pom.xml文件,也没有使用Maven。 那么如何排除绑定呢?

  • 当我尝试在eclipse中运行junit测试时,出现以下错误: SLF4J:类路径包含多个SLF4J绑定。SLF4J:在[jar: file:/C:/User/MaximilianBecker/. m2/repository/uk/org/lidalia/slf4j-test/1.2.0/slf4j-test-1.2.0.jar中找到绑定! /org/slf4j/impl/StaticLogger