jar包衝突問題

這兩天在啓動一個新項目的時候,項目一直啓動不了,報StackOverFlow;html

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>

再啓動試試:java

仍是報錯,難道咱們的配置沒有生效,或者緩存沒有從新BUILD,各類騷操做以後仍是不行,web

由於這個項目生產上是已經部署的,因此代碼應該沒有問題,仍是好好看日誌吧,spring

無心中發現一個警告信息:apache

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項目啓動後,突然發現有下邊的警告:tomcat

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

問題隱患:

  固然最關心的是它是否有隱患,若是你在開發工具中運行,對,沒毛病,通常會正常啓動。
  通過我使用狀況中的觀察,貌似springboot 配置成tomcat運行 ,即修改爲war 包以後,通常這個警告沒有什麼影響;可是若是是傳統的jar 包,儘管你在開發工具中能正常運行,也可能在打完包以後不能運行。ruby

問題出現:

  由於咱們是分佈式項目開發,服務層做爲一個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而致使的!
相關文章
相關標籤/搜索