Getting NoSuchMethodError: javax.servlet.ServletCo

2019-01-14 10:28发布

I am getting below exception when I am trying to run a Spring MVC application using Spring boot...

ContainerBase: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:188)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:799)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    ... 6 more
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.addServlet(Ljava/lang/String;Ljavax/servlet/Servlet;)Ljavax/servlet/ServletRegistration$Dynamic;
    at org.springframework.boot.context.embedded.ServletRegistrationBean.onStartup(ServletRegistrationBean.java:166)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:214)
    at org.springframework.boot.context.embedded.tomcat.ServletContextInitializerLifecycleListener.lifecycleEvent(ServletContextInitializerLifecycleListener.java:54)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5355)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 6 more

15条回答
Viruses.
2楼-- · 2019-01-14 11:06

If you are using STS and you have added the Groovy library to your project (Project Properties -> Java Build Path -> Libraries), then make sure that you select the "No, only include groovy-all" option. The other option of "Yes, include groovy-all and bsf.jar ..., servlet-2.4.jar" adds servlet-2.4.jar which conflicts with the embedded tomcat8 classes resulting in this problem.

查看更多
聊天终结者
3楼-- · 2019-01-14 11:09

I also faced this error. I have a Maven/Spring MVC/Spring Boot project. I'm using IntelliJ as the IDE and whenever i add new dependancies to the POM.xml the IDE alters the .iml file (as expected) but what's weird is that it moves the following line to the top of the file:

<orderEntry type="library" name="Java EE 6-Java EE 6" level="project" />

once that happens my project will not compile and I get the same error:

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

by simply moving the orderEntry for Java EE back to the bottom the error goes away and I can compile once more.

查看更多
We Are One
4楼-- · 2019-01-14 11:11

I had this in a spring-boot webapp, only in the deployment server (was running fine on my local machine). I solve it by adding:

<dependencies>
<!-- … -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<!-- … -->

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html

查看更多
别忘想泡老子
5楼-- · 2019-01-14 11:11

According to this solution: https://stevewall123.wordpress.com/2015/04/17/spring-boot-application-and-tomcat-error-a-child-container-failed-during-start/

You can change the Tomcat version by setting the "tomcat.version" property:

ext['tomcat.version'] = '7.0.63' //my version of Tomcat

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
}

it works for me, thanks to the author of that article.

Cheers :)

查看更多
我想做一个坏孩纸
6楼-- · 2019-01-14 11:13

I am using Jetty in my project and got the same error. My quick solution was to exclude embedded Tomcat from the Maven dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-ws</artifactId>
    <version>1.2.0.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
查看更多
We Are One
7楼-- · 2019-01-14 11:13

mvn dependency:tree did not reveal the servlet-api.jar on my classpath, but right-clicking the project (in Eclipse) and going to Build Path / Configure Build Path showed that my default workspace JRE, JDK 1.7.0_51, had that jar in its jre/lib/ext directory (at least in my installation). I tried and failed to remove it from my classpath. @Pedro's solution of forcing the tomcat version to 7 worked. So did installing the latest version of Java 7: JDK 1.7.0_79.

查看更多
登录 后发表回答