Tomcat启动报java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()异常
借助jdk提供的javap反编译命令和maven提供的依赖树分析命令,快速定位到了问题点。
把SpringBoot项目代码打包成jar包,运行jar包,启动tomcat,报应用程序期启动异常。
错误信息
收集的错误信息如下
2024-08-07 10:20:36.146 [main] DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception
java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
at org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1319)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardPipeline.startInternal(StandardPipeline.java:176)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5147)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:835)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:263)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:432)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:927)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.startup.Tomcat.start(Tomcat.java:486)
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:123)
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:104)
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:479)
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:211)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:184)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:162)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
at spdb.dlp.ScanControl.ApplicationStartup.main(ApplicationStartup.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)
2024-08-07 10:20:36.146 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter -
***************************
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:1319)
The following method did not exist:
javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
The calling method's class, org.apache.catalina.authenticator.AuthenticatorBase, was loaded from the following location:
jar:file:/env/xxx.jar!/BOOT-INF/lib/tomcat-embed-core-9.0.63.jar!/org/apache/catalina/authenticator/AuthenticatorBase.class
The called method's class, javax.servlet.ServletContext, is available from the following locations:
jar:file:/env/xxx.jar!/BOOT-INF/lib/servlet-api-2.4.jar!/javax/servlet/ServletContext.class
jar:file:/env/xxx.jar!/BOOT-INF/lib/tomcat-embed-core-9.0.63.jar!/javax/servlet/ServletContext.class
The called method's class hierarchy was loaded from the following locations:
javax.servlet.ServletContext: jar:file:/env/xxx.jar!/BOOT-INF/lib/servlet-api-2.4.jar!/
Action:
Correct the classpath of your application so that it contains compatible versions of the classes org.apache.catalina.authenticator.AuthenticatorBase and javax.servlet.ServletContext
问题分析
从日志中看,是servlet-api-2.4.jar的javax.servlet.ServletContext类中缺少getVirtualServerName()方法。解压这个jar包取出javax.servlet.ServletContext类,用javap命令反编译
javap ServletContext.class
得到反编译后的内容,如下,从下面方法列表中,确实没有找到getVirtualServerName()方法,是不是类加载的不对?
Compiled from "ServletContext.java"
public interface javax.servlet.ServletContext {
public abstract javax.servlet.ServletContext getContext(java.lang.String);
public abstract int getMajorVersion();
public abstract int getMinorVersion();
public abstract java.lang.String getMimeType(java.lang.String);
public abstract java.util.Set getResourcePaths(java.lang.String);
public abstract java.net.URL getResource(java.lang.String) throws java.net.MalformedURLException;
public abstract java.io.InputStream getResourceAsStream(java.lang.String);
public abstract javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String);
public abstract javax.servlet.RequestDispatcher getNamedDispatcher(java.lang.String);
public abstract javax.servlet.Servlet getServlet(java.lang.String) throws javax.servlet.ServletException;
public abstract java.util.Enumeration getServlets();
public abstract java.util.Enumeration getServletNames();
public abstract void log(java.lang.String);
public abstract void log(java.lang.Exception, java.lang.String);
public abstract void log(java.lang.String, java.lang.Throwable);
public abstract java.lang.String getRealPath(java.lang.String);
public abstract java.lang.String getServerInfo();
public abstract java.lang.String getInitParameter(java.lang.String);
public abstract java.util.Enumeration getInitParameterNames();
public abstract java.lang.Object getAttribute(java.lang.String);
public abstract java.util.Enumeration getAttributeNames();
public abstract void setAttribute(java.lang.String, java.lang.Object);
public abstract void removeAttribute(java.lang.String);
public abstract java.lang.String getServletContextName();
}
于是打开项目代码,用IDEA搜ServletContext这个类,搜到两个结果,一个是tomcat-embed-core-9.0.63提供的,一个是servlet-api-2.4提供的。
打开servlet-api-2.4中的ServletContext.class文件,搜索getVirtualServerName()方法,没有搜到。
打开tomcat-embed-core-9.0.63中的ServletContext.class文件,搜索getVirtualServerName()方法,搜到一个符合的结果,此结果的注释中说明此方法是自Servlet 3.1才有的,
从上面结果推测,tomcat-embed-core-9.0.63版本较高,需要依赖高版本ServletContext才有的getVirtualServerName()方法,所以启动时正常情况应该要加载它本身携带的ServletContext类,但是项目在启动过程中优先加载了servlet-api-2.4中低版本的ServletContext类。
那是不是想办法把servlet-api-2.4这个依赖去掉就可以了?
解决方案
从lib库中移除servlet-api
在lib库中移除servlet-api-2.4.jar后,服务可以正常启动。但此方法每次打包后需要人为参与将其移除,从易用性角度来讲,这个方案是不适合的。
从项目移除servlet-api
移除前要知道这个servlet-api-2.4.jar从哪里引入的。要知道从哪里引入,需要分析项目依赖树,可以使用maven的dependency命令分析项目依赖树。
mvn dependency:tree
从输出的依赖树中得到下面这一段,可以看出javax.servlet:servlet-api:jar:2.4:compile是从net.sf.json-lib:json-lib-ext-spring:jar:1.0.2:compile中引入的,
[INFO] xx.yy.zz:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-autoconfigure:jar:2.7.0:compile
[INFO] +- net.sf.json-lib:json-lib-ext-spring:jar:1.0.2:compile
[INFO] | +- net.sf.json-lib:json-lib:jar:jdk15:2.2.2:compile
[INFO] | | +- commons-beanutils:commons-beanutils:jar:1.7.0:compile
[INFO] | | +- commons-collections:commons-collections:jar:3.2:compile
[INFO] | | +- commons-lang:commons-lang:jar:2.4:compile
[INFO] | | +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] | | \- net.sf.ezmorph:ezmorph:jar:1.0.4:compile
[INFO] | +- javax.servlet:servlet-api:jar:2.4:compile
[INFO] | \- log4j:log4j:jar:1.2.14:runtime
把net.sf.json-lib:json-lib-ext-spring:jar:1.0.2:compile拆解为常见的mvaen格式
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib-ext-spring</artifactId>
<version>1.0.2</version>
<dependency>
打开pom.xml,找到上面这一段内容,点进去后看到servlet-api,
知道servlet-api-2.4在json-lib-ext-spring这个位置后,使用exclusions标签将其排除,刷新maven,重新编译后能正常运行。
总结
借助jdk提供的javap反编译命令和maven提供的依赖树分析命令,快速定位到了问题点。
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)