小结:解决jar包冲突

小结:解决jar包冲突

1 jar包冲突定义(个人理解)

一个jar包会依赖于其它的jar包,如果依赖的jar包的版本不正确或者绑定了同一个类,就可能会报错。

而在java的maven工程中,项目会引入多个jar包,而它们依赖的特定版本的jar包也会一起引入进来,但是这些被依赖的jar包之间会有冲突,比如:版本,java类等等,这就是jar包冲突。

2 jar包冲突的特点

  • 普遍性
  • 比较复杂
  • 影响程序正常启动

3 解决步骤

3.1 锁定引起冲突的jar包

通过日志信息

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/1_java/software/repository/org/slf4j/slf4j-simple/1.7.26/slf4j-simple-1.7.26.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/1_java/software/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.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.SimpleLoggerFactory]
Disconnected from the target VM, address: '127.0.0.1:50094', transport: 'socket'
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.SimpleLoggerFactory loaded from file:/D:/1_java/software/repository/org/slf4j/slf4j-simple/1.7.26/slf4j-simple-1.7.26.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.impl.SimpleLoggerFactory

以如上报错信息为例,我们可以了解到:

SLF4J 要引入的 StaticLoggerBinder.classslf4j-simple-1.7.26.jarlogback-classic-1.2.3.jar 这两个jar包里都有提供,它不知道要用哪个了。

所以我们可以锁定了:干掉 slf4j-simpleslf4j-simple

****3.2 理清相关jar包的依赖

我们可以用idea软件的show dependences… 功能,查看jar包间的依赖关系:
小结:解决jar包冲突_第1张图片

顺着下路走,直到发现我们在 pom.xml 文件中引入的那个依赖的包名。

我这里找到是以下这个

小结:解决jar包冲突_第2张图片

3.3 去除jar包

在pom.xml文件中做如下修改,以排除掉依赖的jar包

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
    <exclusions>
        <exclusion>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-simpleartifactId>
        exclusion>
    exclusions>
dependency>

你可能感兴趣的:(小结,java,maven,jar,slf4j,bug)