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
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
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:
Your servlet version is older than 3.1.
Some other jar has the servlet with a version older than 3.1.
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.
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>
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.
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>
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.
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
Assuming this problem appears when you ran the application in Eclipse.
Use Dependency Hierarchy view to search for servlet-api in pom.xm
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.
I attach gradle style dependencies code.
dependencies {
compileOnly("javax.servlet:javax.servlet-api:3.1.0")
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')
Related
I have a spring boot application that works fine when I run it using the embedded server from Intellj. However, when I package it into .war file and deploy it on tomcat I get the following error:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/file-upload-0.0.1-SNAPSHOT]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
at
org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:985)
at
org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: More than one fragment with the name [spring_web] was found. This is not legal with relative ordering. See section 8.2.2 2c of the Servlet specification for details. Consider using absolute ordering.
at org.apache.tomcat.util.descriptor.web.WebXml.orderWebFragments(WebXml.java:2200)
at
org.apache.tomcat.util.descriptor.web.WebXml.orderWebFragments(WebXml.java:2159)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1124)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:769)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
at
org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5176)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 10 more
21-Jan-2019 01:51:04.709 SEVERE [localhost-startStop-1]
org.apache.catalina.startup.HostConfig.deployWAR Error deploying web application archive [C:\Users\dennismo\Dev\Projects\Production Prep\file-upload-module\webapps\file-upload-0.0.1-SNAPSHOT.war]
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/file-upload-0.0.1-SNAPSHOT]]
at
org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:758)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:985)
at
org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
My project does not contain multiple web-fragment so I don't know what could be causing this problem.
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from salaryPaymentRequestRepo -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<akka.version>2.5.17</akka.version>
</properties>
<repositories>
<repository>
<id>default</id>
<url>http://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.univocity</groupId>
<artifactId>univocity-parsers</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>26.0-jre</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.4.2</version><!-- or latest version -->
<exclusions>
<exclusion>
<artifactId>javassist</artifactId>
<groupId>org.javassist</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.12</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-stream_2.12</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-persistence_2.12</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-testkit_2.12</artifactId>
<version>${akka.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/ojdbc7-12.1.0.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
add <absolute-ordering /> tag to your web.xml just under the <display-name> tag.
should work.
Cleaning the server worked for me. Just do the following :
Right click on the server -> Clean.
Then redeploy the app on the server.
Sounds like you have two different versions of Spring, check your dependencies to see if you have a dependency to another Spring-version.
mvn dependency:tree -Dverbose -Dincludes=groupId:artifactId
I had the same problem when building a war file with maven because I did not clean before packaging and maven was including multiple versions of spring jar files from previous builds.
This error seems to happen after project's spring framework update, clearing the project history file in tomcat webapps may solve the problem.
This problem was solved by adding absolute ordering of deployment descriptors.
The tag <absolute-ordering/> was added to the web.xml
Reference:
https://www.oracle.com/technetwork/articles/javaee/javaee6overview-part2-136353.html
I had this occur after I updated the Spring Framework version in a Maven project. I was finally able to resolve the issue by running mvn clean, apparently the old Spring JARs were being picked up somewhere under target.
This error generally comes from that if we have two copy of spring in our container, which get loaded by different class loaders(in my case shared class loader and webapp class loader).
Check effective pom, if you have any bundle embed spring jars/classes, if so remove it, you should use the one installed as bundle from container.
If you manually adds all the Jars then you have to delete WEB-INF/lib/spring-web-5.2.9.RELEASE-sources.jar or similar jar file (may be your jar version will be different). Once you delete that jar tomcat will start working.
My project is Springboot based version (1.5.21.RELEASE) and generating war and deploying in physical tomcat.
I had gotten the same issue and got resolved with below approach.
Solution: Exclude tomcat-embed-websocket dependency from spring-boot-starter-web in the pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
<exclusions>
**<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
</exclusion>**
</exclusions>
</dependency>
Just an answer from newbbie to newbbies:
Make sure you place web.xml inside /WebContent/WEB-INF.
I placed it inadvertently in /WebContent (not /WEB-INF), and almost got crazy trying to unsuccessfully apply some of the proposed solutions, the error message was exactly the same.
For me this meant that I was including some "shaded jars" that bundled copies of spring within them. So all the same "spring version" but multiple copies of spring causes this.
You can check if this is the case by going to tomcat/webapps/your_webapp/WEB-INF/lib, unjarring all the jars, and searching for "spring_web"
ex:
# first unjar everything then
WEB-INF/lib $ grep spring_web . -r
spring-web-4.3.12.RELEASE/META-INF/web-fragment.xml
7: <name>spring_web</name>
some_other_shaded_jar/META-INF/web-fragment.xml
7: <name>spring_web</name>
If this is the case you'd need to use the absolute fix of the other answers or not include multiple copies of spring somehow.
Seems this is a change introduced in tomcat 7.0.93 FWIW (so it didn't throw this failure in earlier versions of tomcat 7)
Your web.xml should look like this (added <absolute-ordering/> inside the tag web-app tag)
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<absolute-ordering/>
</web-app>
I had the same problem and solved it. Got similar errors but not used spring boot. I have used spring 3.x with java 8. If above solutions are not working change the jars and search whether those jars are compatible with the java version you are using or not. spring 3.x is not compatible with java 8.
I solved similar problem by removing all my apps (I had there two different apps) from webapps folder and then redeployed my war file.
I had to add this in web.xml to make it work :
<absolute-ordering>
<name>fragment_name_if_required</name>
<others></others>
</absolute-ordering>
All these answers are great but also check if "Apache CXF" lib is in your classpath, it apparently conflicts with Spring Web 5.2.3, for me I temporary removed because I'm actually not using it and now my project is running okay.
For me, this issue happened when Tomcat was not properly installed .I have reinstalled and updated the installation path at:
eclipse window->preferences->runtime ENV -> path of the newly installed tomcat
After setting the correct path, it worked fine.
For me , it was because of 2 spring-web jars i.e. spring-web-5.02-release.jar and spring-web-sources-5.02-release.jar in build path. By removing spring-web-sources-5.02-release.jar, issue is solved.
Had the same issue and wanted to get to the root cause. I knew spring-web jar's fragment is called 'spring_web'. However my source code did not have 'spring_web' fragment anywhere else. So I used a tool (jfind) to search for 'spring_web' through an expanded form of my EAR that was generated at build time. Surprisingly, 'spring_web' was found in another project's web-fragment jar that was generated at build time. On resolving this second occurrence, the issue was gone. So it seems to be due to duplicate 'spring_web' fragment and we need to have just one.
If you have previously built the project with another version and then changed the version and tried to build again, this exception is thrown. You can delete the target folder in your application. If you still get an exception, you can go to the project directory from the terminal and type mvn clean install
Remove all the unused dependencies from the lib folder.
Now try to rerun the application on the server.
Hope it will work now.
I was fighting with this few hours. Solution for me was to invalidate IntelliJ cache.
This problem showed up when I was in the process of upgrading spring framework.
Intellij was packaging two versions of the same library and that's why I was getting an error.
This happened to me when I changed the version of one of the dependencies in pom.xml, undeploying and deploying the webapp fixed it for me
See the below Line in error log:
Caused by: java.lang.IllegalArgumentException: More than one fragment with the name [spring_web] was found. This is not legal with relative ordering. See section 8.2.2 2c of the Servlet specification for details. Consider using absolute ordering.
which says to use absolute ordering.
So, you have to add <absolute-ordering/> tag in your web.xml file inside <web-app> tag.
I have just started to play with Spring MVC. I installed Intellij Idea and Tomcat server and then created a new project from SpringMVC template. When I run it I got the error:
Servlet.init() for servlet mvc-dispatcher threw exception
I solved it by changing Java jdk from version 1.8 to 1.7. When I run it then, I got this error:
java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.pages.hello_jsp
To fix it I had to remove:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
from my pom.xml file and now it's working. Can someone tell me why this template won't work without these changes?
Without more detail about the error you got, I can't answer why you needed to change the Java version.
As for the servlet-api dependency, that is because Tomcat has its own implementation of the servlet-api included. So there was a clash. What you will want to do is add the dependency back into your POM with a scope of provided. That way it is there for your IDE to use as well as at build time. (Unless of course you are getting the servlet-api from some where else such as the javaee-api dependency).
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
This question already has answers here:
The method getDispatcherType() is undefined for the type HttpServletRequest
(3 answers)
Closed 6 years ago.
I have imported a Java dynamic web project into the Eclipse IDE (which was implemented in Eclipse IDE and properly working).
I'm getting a "getDispatcherType() is undefined for the type HttpServletRequest" error while running the project.
I have copied every file into IDE as per the structure and the work is done.
Now I just want to know why I am getting this error when I have imported the project. Did anyone faced the same issue? Please let me know what mistake I might have made.
I had the same issue when I had a conflicting servlet-api version I was using in IntelliJ that conflicted with what was supported in Tomcat 8.0.x... I was using Maven, so I just changed my dependency to this, then did a clean deploy of my webapp and the problem went away.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
tomcat 8.0.18, maven. It's about lib conflict. My solution is:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
changed to:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
You should exclude "servlet-api-2.5.jar" from any other dependency that you may have in your pom.xml.
Try not to add a different servlet-api as compile, as your tomcat already provides it for you.
My steps:
I have checked that there was a servlet-api-2.5.jar being included in my WEB-INF/lib folder by Maven, so then, I checked the full dependency graph on "Maven projects #IntelliJ Idea", then I excluded this dependency from ALL the places where it comes from. [ The button "Show dependencies" comes handy for this ]
I had to exclude "commons-logging" (as it has servlet-api 2.5 dependency) from velocity-tools.
Also had to exclude servlet-api from jaxws-spring which has a direct dependency on default scope.
Then, just add the scope provided as you should on your javax.servlet-api dependency.
If you add your servlet-api 3.0.1+ as "compile", you may end up with both, and the first to load will win, which is not good at all.
Note:
My guess is that this problem comes from the renaming of the groupId/artifactId of servlet-api, and not being overriden with the oldest version included on maven project. :\
I solved this problem by using the servlet-api.jar and jsp-api.jar from tomcat itself, so the dependency will be specified with system scope like below:
<servlet.api.jar.path>/opt/apache-tomcat-8.0.15/lib/servlet-api.jar</servlet.api.jar.path>
<jsp.api.jar.path>/opt/apache-tomcat-8.0.15/lib/jsp-api.jar</jsp.api.jar.path>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0</version>
<scope>system</scope>
<systemPath>${servlet.api.jar.path}</systemPath>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>system</scope>
<systemPath>${jsp.api.jar.path}</systemPath>
</dependency>
If your tomcat is version 8 use:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
worked!
This can also happen when upgrading from an old to newer Tomcat version and keeping the old jar files such as j2ee.jar and javaee.jar.
j2ee.jar should nerver be include in your webapp, the interfae is implemented by tomcat
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
If you want to find out where the class is being loaded from, try
java -verbose:class -jar foo.jar | grep javax.servlet.ServletContext
where foo.jar is the fat JAR produced by Gradle or Maven. For example, the ServletContext class could be getting read from an older servlet-api JAR in a JDK extensions directory instead of your Maven or Gradle dependencies.
The output of the command looks something like this...
$ java -verbose:class -jar build/libs/foo-0.2.3.jar | grep javax.servlet.ServletContext
[Loaded javax.servlet.ServletContext from jar:file:.../build/libs/foo-0.2.3.jar!/lib/javax.servlet-api-3.1.0.jar!/]
[Loaded javax.servlet.ServletContextListener from jar:file:.../build/libs/foo-0.2.3.jar!/lib/javax.servlet-api-3.1.0.jar!/]
[Loaded javax.servlet.ServletContextAttributeListener from jar:file:.../build/libs/foo-0.2.3.jar!/lib/javax.servlet-api-3.1.0.jar!/]
I solved it excluding a transitive servlet-api dependency.
In my case, it was com.github.isrsal:spring-mvc-logger
<dependency>
<groupId>com.github.isrsal</groupId>
<artifactId>spring-mvc-logger</artifactId>
<version>0.2</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
gradle solution.
I had a similar problem in my lib jar which for some reason brought with it an old version of javax.servlet.ServletContext which was later loaded by my spring-boot module instead of its own supplied class, by that causing a NoSuchMethodError
I fixed it by editing the build.gradle of my lib module :
configurations {
provided.all*.exclude group: 'javax.servlet'
}
For anyone else who couldn't solve it by excluding servlet-api , here is an alternative:
It turns out Spring Boot defaults to Tomcat 8. If you're running a different version of Tomcat and would like to fix this error, just add your tomcat version to the pom properties:
<properties>
<tomcat.version>7.0.63</tomcat.version>
</properties>
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
I had this problem when trying to start my spring boot server during tests (using gradle + spock). I traced the problem to the wiremock library.
This fixed it:
testCompile('com.github.tomakehurst:wiremock:1.58') {
exclude module: 'servlet-api' // this exclude fixed it
}
FYI
gradle dependencies
shows (abridged):
\--- com.github.tomakehurst:wiremock:1.58
+--- org.mortbay.jetty:jetty:6.1.26
+--- org.mortbay.jetty:jetty-util:6.1.26
\--- org.mortbay.jetty:servlet-api:2.5-20081211
The ancient (2008) org.mortbay.jetty:servlet-api jar contains a version of ServletContext that is incompatible with version 1.3.2 of spring boot (worked OK at least up to 1.2.6).
I was using Gradle and what worked for me:
configurations {
all*.exclude group: '', module: 'servlet-api'
}
It trimmed dependency tree in desired way.
I was running with Hadoop 2.7.2 using Spring-boot, the following Hadoop dependencies uses javax.servlet which prevented the embedded tomcat version to start. Below exclusions in my POM fixed the issue.
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.2</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-hadoop-boot</artifactId>
<version>2.3.0.RELEASE-hadoop26</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.2</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
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 :)
I couldn't track down the problematic reference except for knowing it is somehow getting into my class-path so here is my 2 cents for lazy developers using eclipse:
Scroll down the list of maven dependencies under your project tree (usually under Java Resources -> Libraries -> Maven Dependencies), track the problematic added jar you wish to exclude from your packaged JAR, right click on it -> select Maven -> Exclude Maven Artifact !
Voila - automatically an exclusion will be added to your pom right under the dependency that refers it.
Mine BTW was jcifs...
<dependency>
<groupId>org.codelibs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.18.2</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
Good luck!
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.
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>
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.
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.
For quick solution, I have removed the servlet-api.jar manually from the lib and than build the application and it works.Though, ideally as suggested by Kumetix, one should needs to scrutinize till the dependency which causing it to load in classpath.
For me this was solution;
https://mkyong.com/java/servlet-api-2-5-jar-jar-not-loaded/
adding the dependency of the servlet-api to pom.xml
Is it ok to take it from Glassfish project ( glassfish-persistence-api) or may be there is a Hibernate jar?
If you are using maven, adding below dependency should work
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
If you are developing an OSGi system I would recommend you to download the "bundlefied" version from Springsource Enterprise Bundle Repository.
Otherwise its ok to use a regular jar-file containing the javax.persistence package
You can use the ejb3-persistence.jar that's bundled with hibernate. This jar only includes the javax.persistence package.
In the latest and greatest Hibernate, I was able to resolve the dependency by including the hibernate-jpa-2.0-api-1.0.0.Final.jar within lib/jpa directory. I didn't find the ejb-persistence jar in the most recent download.
hibernate.jar and hibernate-entitymanager.jar contains only the packages org.hibernate.*. So you should take it from the Glassfish project.
For JPA 2.1 the javax.persistence package can be found in here:
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
See: hibernate-jpa-2.1-api on Maven Central
The pattern seems to be to change the artefact name as the JPA version changes. If this continues new versions can be expected to arrive in Maven Central here: Hibernate JPA versions
The above JPA 2.1 APi can be used in conjunction with Hibernate 4.3.7, specifically:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.7.Final</version>
</dependency>
In general, i agree with above answers that recommend to add maven dependency, but i prefer following solution.
Add a dependency with API classes for full JavaEE profile:
<properties>
<javaee-api.version>7.0</javaee-api.version>
<hibernate-entitymanager.version>5.1.3.Final</hibernate-entitymanager.version>
</properties>
<depencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee-api.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
Also add dependency with particular JPA provider like antonycc suggested:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate-entitymanager.version}</version>
</dependency>
Note <scope>provided</scope> in API dependency section: this means that corresponding jar will not be exported into artifact's lib/, but will be provided by application server. Make sure your application server implements specified version of JavaEE API.