41

I am facing an issue during deployment of a service in Tomcat 8. Getting following error :

Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String; at org.apache.tomcat.websocket.server.WsServerContainer.(WsServerContainer.java:149) at org.apache.tomcat.websocket.server.WsSci.init(WsSci.java:131) at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:47) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5244) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 10 more

Method getVirtualServerName was introduced in Servlet 3.1 and after extracting MANIFEST.MF from my servlet-api jar I got following details :

Specification-Title: Java API for Servlets 
Specification-Version: 3.1 
Specification-Vendor: Sun Microsystems, Inc. 
Implementation-Title: javax.servlet 

Which says that its having 3.1. So is there any other reason for this error? Please help

8
  • do you run from eclipse? or ... please let us know more about your environment, application, and settings. Jan 22, 2016 at 15:31
  • Are you definitely using Tomcat 8? Your post is tagged 'tomcat7'... Jan 22, 2016 at 15:35
  • @AndrewRegan Sorry it was a mistake. I edited. I am using tomcat 8
    – Rahman
    Jan 22, 2016 at 15:39
  • 1
    what I was thinking too, thats why I asked about the environment... do you use pom.xml ? maybe you should change the property to <tomcat.version>xxxx</tomcat.version> to tomcat 8... ? Jan 22, 2016 at 15:39
  • @MrSimpleMind Yes I am using POM. But I have not declared <tomcat.version> . So if I dont declare what it will take by default ?
    – Rahman
    Jan 22, 2016 at 15:41

11 Answers 11

27

Check all your Maven (or equivalent) dependencies and make sure that you - or most likely another dependency - are not pulling in a pre-3.1 version of the javax.servlet / servlet-api that may be taking precedence over what's in your Tomcat 8. If you've manually deployed, make sure you haven't manually copied any servlet-api JARs into Tomcat itself.

See: https://stackoverflow.com/a/26232535/954442

6
  • 6
    Locate the offending dependency which is bringing in the servlet-api jar in maven with the command mvn dependency:tree from the command line in the root of your project, search the output for the servlet-api its most likely 2.? version , exclude it from the dependency with the following in your pom. { <exclusion> <artifactId>servlet-api</artifactId> <groupId>javax.servlet</groupId> </exclusion>} Aug 21, 2017 at 9:48
  • @user1412523 Thanks! This solved it for me. My dependency was google-oauth-client-jetty I removed it as I did not need it anyway and it's working again.
    – A.W.
    Sep 8, 2018 at 12:26
  • my dependency was hadoop-common (2.9.2)
    – Fabich
    Jun 4, 2019 at 13:16
  • mvn dependency:tree | grep servlet-api
    – kinjelom
    Mar 30, 2020 at 14:37
  • Thanks @user1412523, that was a really useful comment. I could finally get my apache fop-transcoder-allinone 2.4 dependency fixed and can move on now. May 3, 2020 at 20:50
22

The method getVirtualServerName has been added in ServletContext in Servlet 3.1. See the java doc's method getVirtualServerName.

This problem has 3 primary causes:

  1. Your servlet version is older than 3.1.

  2. Some other jar has the servlet with a version older than 3.1.

  3. Your tomcat version is older than 8

to solve it, you can try the below way.

I. Check your pom.xml for the code below.

  <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
       <version>3.1.0</version>
    </dependency>

if your pom.xml has the above code, it would still has that problem. you can do the second way.

II. to check your other jar has refer to the javax.servlet-api jar. for example, the org.apache.santuario has refer to the javax.servlet-api jar. the pom.xml:

<dependency>  
    <groupId>org.apache.santuario</groupId>  
    <artifactId>xmlsec</artifactId>  
    <version>1.4.3</version>   
</dependency> 

but when you look at the maven dependencies, it refer to the javax.servlet-api jar whose version is 2.3 older than 3.1.

enter image description here

so you should exclude the 2.3 version. pom.xml:

<!-- exclude servlet-api 2.3 jar-->  
<dependency>  
    <groupId>org.apache.santuario</groupId>  
    <artifactId>xmlsec</artifactId>  
    <version>1.4.3</version>  
    <exclusions>  
        <exclusion>  
            <groupId>javax.servlet</groupId>  
            <artifactId>servlet-api</artifactId>  
        </exclusion>  
    </exclusions>  
</dependency>  

<!-- servlet-api 3.1 version has getVirtualServerName() -->  
<dependency>  
    <groupId>javax.servlet</groupId>  
    <artifactId>javax.servlet-api</artifactId>  
    <version>3.1.0</version>  
</dependency> 

III. spring boot run the default tomcat 7. so define your tomcat version 8 instead of tomcat 7. so add the code your pom.xml:

   <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <tomcat.version>8.5.5</tomcat.version>
    </properties>
1
  • It is ok not to exclude transitive dependency, but just include the correct version. Maven takes the one which is closer in graph.
    – senyor
    Sep 3, 2019 at 16:54
10

I had this error on IntelliJ with maven after updating IntelliJ.

I could run the tests with maven but not from my IDE.

I solved the problem by removing the ./idea and project.iml files and reloading the project.

0
5

If you have used this dependency:

<dependency>
    <groupId>com.google.oauth-client</groupId>
    <artifactId>google-oauth-client-jetty</artifactId>
    <version>1.23.0</version>
</dependency>

Then please exclude as below:

<dependency>
    <groupId>com.google.oauth-client</groupId>
    <artifactId>google-oauth-client-jetty</artifactId>
    <version>1.23.0</version>
    <exclusions>
        <exclusion>
            <artifactId>servlet-api</artifactId>
            <groupId>org.mortbay.jetty</groupId>
        </exclusion>
    </exclusions>
</dependency>
2
  • Great that was exactly my error, how did you know that ??
    – Rigoxls
    Nov 27, 2020 at 16:19
  • @Rigoxls I remember I encountered this problem while I was working with Google client APIs and Spring Framework
    – yuen26
    Nov 28, 2020 at 8:08
4

Spring boot will run tomcat 7 per default, you have to override maven build tomcat.version in your pom.xml. See below to run tomcat 8.0.30

<properties>
  <tomcat.version>8.0.30</tomcat.version>
</properties>

Should fix your problem.

2

Solved On my mac with java 8 was facing issue with downloaded tomcat from site and unzip.

My issue got solved because there was a extra servlet-api.jar file which was getting picked up. It was coming from /Library/Java/Extensions/servlet-api.jar

For finding it in your system you can use sudo find / -name servlet-api.jar

Removed it by backing it up somewhere else.

I was following this for intallation https://gist.github.com/ddanailov-nmdp/c97aba2ca926b9627f6b4f7174083a32

1

Assuming this problem appears when you ran the application in Eclipse. Use Dependency Hierarchy view to search for servlet-api in pom.xm

0

After a huge pain & sifting through all these stackoverflow answers the only thing that ended up working for me was downgrading from tomcat8 to tomcat7. I know this isn't an ideal solution, and perhaps it was just a fresh install of tomcat that solved my problem. If all else fails give that a shot.

0
0

I attach gradle style dependencies code.

dependencies {
    compileOnly("javax.servlet:javax.servlet-api:3.1.0")
1
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. May 6, 2022 at 16:06
0

In my case, I add vm option -verbose:class to find the wrong jar and excluded the conflicted jar then add the higer version of javax.servlet-api(3.1.0), the problem was sovled.

-1

This surely has something to do with the version of javax.servlet and version of Tomcat.

In my case, it went away when I declared javax.servlet dependency in gradle with no version. Like this -

compile('javax.servlet:servlet-api')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.