The following method did not exist:javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;


SpringBoot 啟動失敗。又是jar沖突,這是第幾次了?

報錯細節

報錯細節:

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1355)

The following method did not exist:

    javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

The method's class, javax.servlet.ServletContext, is available from the following locations:

    jar:file:/opt/ETC/xxl-job-executor-etc/run/lib/servlet-api-2.5.jar!/javax/servlet/ServletContext.class
    jar:file:/opt/ETC/xxl-job-executor-etc/run/lib/tomcat-embed-core-9.0.41.jar!/javax/servlet/ServletContext.class

The class hierarchy was loaded from the following locations:

    javax.servlet.ServletContext: file:/opt/ETC/xxl-job-executor-etc/run/lib/servlet-api-2.5.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of javax.servlet.ServletContext

分析錯誤

描述信息大概意思是:你在調用一個不存在的方法,該方法可以在xxx位置找到。

what fk ?看到17行18行瞬間反應過來,這個方法在兩個jar中都有,並且類路徑完全相同,jre懵逼了,不知道調用哪個,然后將問題拋出來給開發者:你必須讓我能夠明確知道調用哪個。

接下來事情就明確了,我們需要剔除多余的重復jar,本次示例的沖突是servlet-api,由於SpringBoot默認是使用內嵌的tomcat啟動,所以應該是其他的依賴中重復導入了這個依賴,直接進行排除就OK了。

怎么排出重復依賴?

  • 第一步:檢查pom有沒有顯示的引入沖突的依賴,如果有,remove 它,基本沒問題了。

  • 第二步:如果第一步沒有解決,那就是被其他的依賴導入了,下面就演示一下排出依賴的步驟(以下通過IDEA進行maven依賴排出)

    打開pom.xml(平時我們瀏覽、編輯都是在Text模式下):pom.xml

    切換到Dependency Analyzer模式下:pom.xml

    搜索servlet-api: servlet-api

    排出依賴:右擊選擇的依賴,選擇Exclude

    切換到Text模式,成功排除了沖突依賴:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM