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模式,成功排除了冲突依赖:

posted @ 2021-09-16 20:57  LANGKYE#  阅读(1200)  评论(0编辑  收藏  举报