Java异常 # Class path contains multiple SLF4J bindings.警告解决

1.异常现象

启动 Maven 项目时,抛出警告信息:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/Tp_Mylocal/20_Install/maven/repo/org/slf4j/slf4j-log4j12/1.7.20/slf4j-log4j12-1.7.20.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/Tp_Mylocal/20_Install/maven/repo/ch/qos/logback/logback-classic/1.1.7/logback-classic-1.1.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. 
SLF4J: See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.

2.排查分析

从报出来的警告信息来看,大致意思是,pom.xml 引入的包 slf4j-log4j12-1.7.20.jar 和包 logback-classic-1.1.7.jar 中,都找到了 /org/slf4j/impl/StaticLoggerBinder.class 这个类,不知道用哪个,于是发生了冲突。
 

3.解决方案

既然是 jar 包冲突,那就找到 pom.xml 文件,我们来排除一下:
1) 找到 pom.xml 文件,打开,然后右键,在菜单中选择 [Diagrams] -> [Show Dependencies]在这里插入图片描述
2) 弹出一画复杂交错的依赖图,触目惊心,哭笑不得
哭笑不得与置之不理
3) 攻而克之,Ctrl + F,搜索 slf4j-log4j12,找到 slf4j-log4j12 后,右键,把它 Exclude 掉
在这里插入图片描述
好了,查看 pom.xml 文件,刚刚排除掉的 jar 已经自动生成了 exclusion 代码,如:

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>${zookeeper.version}</version>
    <exclusions>
        <exclusion>
            <artifactId>slf4j-log4j12</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
    </exclusions>
</dependency>

或者,也可以手动在 pom.xml 文件中排除相关依赖。

注意:

  1. log4j-over-slf4j.jarslf4j-log4j12.jar 是跟 Java 日志系统相关的两个 jar 包,当它们同时出现在 classpath 下时,可能会引起堆栈溢出异常。
  2. 如果你用的是 logback 日志,要排除的是 slf4j-log4j12.jar 包,不要排除 logback-classic.jar 包。

猜你喜欢

转载自blog.csdn.net/itanping/article/details/107896393