jar包冲突问题

这两天在启动一个新项目的时候,项目一直启动不了,报StackOverFlow;

java.util.concurrent.ExecutionException: java.lang.StackOverflowError

这个问题第一反应就是启动参数里面JVM永久区配置的小了或者没有配置,好的,直接加上配置

<plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <path>app</path>
                    <port>8080</port>
                    <uriEncoding>UTF-8</uriEncoding>
                    <systemProperties>
                        <JAVA_OPTS>-Xms256m -Xmx2048m -XX:MaxPermSize=1024m</JAVA_OPTS>
                    </systemProperties>
                </configuration>
                <version>2.2</version>
            </plugin>

再启动试试:

还是报错,难道我们的配置没有生效,或者缓存没有重新BUILD,各种骚操作之后还是不行,

因为这个项目生产上是已经部署的,所以代码应该没有问题,还是好好看日志吧,

无意中发现一个警告信息:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/XX/.m2/repository/org/slf4j/slf4j-log4j12/1.7.6/slf4j-log4j12-1.7.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/XX/.m2/repository/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.

是不是Jar冲突了?带着这种想法去查项目,然后各种

<exclusion></exclusion>
在网上看到一个很好的帖子,说的很详细,这里直接贴下原文

 

springboot 关于 Class path contains multiple SLF4J bindings.警告的解决

  有一次配置好springboot项目启动后,忽然发现有下边的警告:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/mavenJarOnline/ch/qos/logback/logback-classic/1.1.9/logback-classic-1.1.9.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/mavenJarOnline/org/slf4j/slf4j-log4j12/1.7.22/slf4j-log4j12-1.7.22.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 [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
原因分析:

  上边的大概意思是说logback-classic 包和slf4j-log4j12 包,关于org/slf4j/impl/StaticLoggerBinder.class 这个类发生了冲突。
  发生这个错误的原因,首先logback 日志的开发者和log4j 的开发者据说是一波人,而springboot 默认日志是,较新的logback 日志。但是在以前流行的日志却是log4j ,而且很多的第三方工具都含有log4j 得引入。
  而我们在项目开发中,难免会引入各种各样的工具包,所以,基本上springboot 项目,如果不注意,肯定会出现这种冲突的。

问题隐患:

  当然最关心的是它是否有隐患,如果你在开发工具中运行,对,没毛病,一般会正常启动。
  经过我使用情况中的观察,貌似springboot 配置成tomcat运行 ,即修改成war 包之后,一般这个警告没有什么影响;但是如果是传统的jar 包,尽管你在开发工具中能正常运行,也可能在打完包之后不能运行。

问题出现:

  因为我们是分布式项目开发,服务层作为一个jar 运行,而接口调用和前端页面,作为一个war 一起运行,也就是说我们即有war ,又有jar ,项目部署的时候,需要打完包之后运行才可以。
  在打完包之后,war 包能正常运行,jar 包不能正常运行,报了如下错误:

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner
.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

Caused by: java.lang.ExceptionInInitializerError
        at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:72)
        at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:45
)
        at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
        at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)

        at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
        at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
        at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogF
actory.java:155)
        at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogF
actory.java:132)
        at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:273)
        at org.springframework.boot.SpringApplication.<clinit>(SpringApplication
.java:190)
        at spingboot.study.SpringbootStudyApplication.main(SpringbootStudyApplic
ation.java:14)
        ... 8 more
Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar A
ND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See
 also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
        at org.slf4j.impl.Log4jLoggerFactory.<clinit>(Log4jLoggerFactory.java:54
)
        ... 19 more
  • 问题解决:

  当然问题我不敢确定一定是因为warjar 的原因,你们也可能不关心这个,我们只想知道如何解决这种问题而已。
  问题解决办法很简单,就是既然抛了jar包冲突 ,那我们就排除一个jar 包即可。关键是排除哪一个jar包 ,这里注意下了,如果你用的是logback 日志,一定要排除slf4j-log4j12 包,不要排除logback-classic 包。
  即找到pom.xml 文件,如果你们的开发工具,比如eclipseidea 都可以看引入jar 包的联系,比如idea可以这样看到你的依赖结构:

 

 
  点击后,弹出下边的这样的结构:

 


  通过上图中,你可以看到zookeeper 包中默认引入了slf4j-log4j12包,除此之外,还有我们springboot 一定引入的spring-boot-starter-web 包,它里边也有这个slf4j-log4j12 引入。
  我们只要在pom.xml 里排除这个即可。
如下:

<dependency>
     <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!--排除这个slf4j-log4j12-->
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
</dependency>

下边是我们项目引入的第三方的工具包中:
<dependency> 
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
<!--排除这个slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>

 

基本上按照这个思路就能解决问题,我这边实际中遇到的就是一个引人的jar中引人了这个jar而导致的!

posted @ 2019-06-14 12:33  龙X  阅读(1837)  评论(0编辑  收藏  举报