· errors  · 2 min read

java.lang.NoSuchMethodError javax.servlet.ServletContext.getVirtualServerName()

Recently while working on my Spring Boot project, I came across an error method not found getVirtualServerName. This error was very frequent and bothersome. During the execution of Spring Boot project, I ran into NoSuchMethodError javax.servlet.ServletContext.getVirtualServerName() exception.  Despite going through maven dependency, it was harder to find out where the servlet-api jar was coming into my project build path. In my handling exceptions post, I showed how to handle exceptions better.

getVirtualServerName

Issue -

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) [na:1.8.0_91]
at java.util.concurrent.FutureTask.get(FutureTask.java:192) [na:1.8.0_91]
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:911) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:890) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1403) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1393) [tomcat-embed-core-8.5.6.jar:8.5.6]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_91]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_91]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_91]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91]
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) [tomcat-embed-core-8.5.6.jar:8.5.6]
... 6 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Failed to start component [Pipeline[StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5099) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.6.jar:8.5.6]
... 6 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.authenticator.NonLoginAuthenticator[]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) [tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.core.StandardPipeline.startInternal(StandardPipeline.java:170) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.6.jar:8.5.6]
... 8 common frames omitted
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
at org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1125) ~[tomcat-embed-core-8.5.6.jar:8.5.6]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.6.jar:8.5.6]
... 10 common frames omitted

Reason -

Depending on the build path, servlet-api jar that exists is not the right version. If it is older than 3.1, it doesn’t contain method getVirtualServerName().

How did I resolve this issue?

I describe the solution separately, but how I analyzed the issue here. I went through servlet-api jar and tried to find the class ServletContext that contains this method. But the Jar file that I had in my project, didn’t contain this method. That’s why the issue. Then it was simple to figure out based on maven dependencies. Once I had a dependency tree, I was able to nail down the dependency that was bringing the old version of servlet-api jar.

Solution -

  1. Change the version of servlet-api jar
  2. Considering this is a spring-boot project, provide a version of tomcat instead of using default tomcat 8.x which spring-boot provides.
  3. In my case, the fix was to remove javaee.jar from the build path which was bringing servlet-api of an older version. When I changed the Java runtime library from 1.8 to 1.7, the project ran like a charm.

Conclusion

In this post, I showed how to resolve the error getVirtualServerName in ServletContext. If you enjoyed this post, subscribe to my blog.

Back to Blog

Related Posts

View All Posts »

Building Saas in 2024

A comprehensive guide to the technology stack and considerations for building a full-stack SaaS application in 2024.

System Thinking

Learn how to think in systems and apply systems thinking to software engineering and architecture decisions.

How Databases Work

How Databases Work

An in-depth exploration of how databases work under the hood, from storage engines to query processing.