1. 程式人生 > 其它 >The following method did not exist:javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

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模式下):

    切換到Dependency Analyzer模式下:

    搜尋servlet-api:

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

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