SpringBoot报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String
自己电脑上一直用IDEA配置的Tomcat跑的SSM项目,今天同事让我跑一下他的SpringBoot项目,就把整个代码包拷给我,使用IDEA导出后却跑步起来,启动报错,一样的代码怎么就跑步起来呢。。。
后来才发现SpringBoot使用内置Tomcat,但是会缺少servlet-api.jar,所以导致getVirtualServerName()方法找不到,一直报错。
报错信息如下:
2021-02-05 14:40:46.801 ERROR org.apache.catalina.core.ContainerBase - A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[/demo]] at java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[?:1.8.0_271] at java.util.concurrent.FutureTask.get(FutureTask.java:192) ~[?:1.8.0_271] at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:941) [tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:872) [tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1421) [tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1411) [tomcat-embed-core-8.5.31.jar:8.5.31] at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) [?:1.8.0_271] at java.util.concurrent.FutureTask.run(FutureTask.java) [?:1.8.0_271] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_271] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_271] at java.lang.Thread.run(Thread.java:748) [?:1.8.0_271] Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[/demo]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) ~[tomcat-embed-core-8.5.31.jar:8.5.31] ... 7 more Caused by: org.apache.catalina.LifecycleException: Failed to start component [Pipeline[StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[/demo]]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) ~[tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166) ~[tomcat-embed-core-8.5.31.jar:8.5.31] ... 8 more Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.authenticator.NonLoginAuthenticator[/demo]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) ~[tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.catalina.core.StandardPipeline.startInternal(StandardPipeline.java:182) ~[tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166) ~[tomcat-embed-core-8.5.31.jar:8.5.31] ... 8 more Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String; at org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1186) ~[tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.catalina.core.StandardPipeline.startInternal(StandardPipeline.java:182) ~[tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166) ~[tomcat-embed-core-8.5.31.jar:8.5.31] ... 8 more
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
上面的这行报错信息很明显是找不到getVirtualServerName()方法,该方法是在servlet-api.jar中,而SpringBoot内置Tomcat中没有找到该jar包,jdk中也没有找到,所以报错,解决方法就是把Tomcat中lib文件夹下面的servlet-api.jar拷贝一份到jdk\jre\lib\ext相应的目录下面去,如下图所示:
重启项目,成功启动。