1

I am getting below error while starting the Tomcat7 server.

org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer

I am just curious to find the root cause of this issue. In what scenario this error happens? I didn't get the error when I use Tomcat Enterprise Edition(TomEE)

In my project ServletContainerInitializer is getting loaded from javeee-api-7.0.jar

I have tried excluding the javax.servlet-api which is getting loaded from javeee-api but thats not working

Raj
  • 115
  • 8
  • Possible duplicate of [SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer](http://stackoverflow.com/questions/15328363/springservletcontainerinitializer-cannot-be-cast-to-javax-servlet-servletcontain) – Walfrat Dec 29 '16 at 21:33
  • I have checked that before posting this question. Servlet api is getting loaded from javeee-api-7.0.jar and even after exluding servlet-api from javeee-api I am getting the error. Error will not happen if I exclude javeee-api dependency. – Raj Dec 29 '16 at 21:41
  • `I am just curious to find the root cause of this issue. In what scenario this error happens? I didn't get the error when I use Tomcat Enterprise Edition(TomEE)` it's because basic tomcat don't have jee implementation on his own, use tomEE or add EE implementations module – Walfrat Dec 29 '16 at 21:44
  • 1
    Thank you Walfrat. I am using Spring and Kundera ORM dependencies . javeee-api-7.0.jar getting loaded as part of Kundera dependency and ServletContainerInitializer is getting loaded from that. Is there a way I can exclude javax.servlet-api from that? – Raj Dec 29 '16 at 21:58
  • Make sure you are using the right `javax.servlet-api` dependency for your version of Tomcat and set is as provided (if you're using Maven). Spring tries to autodetect the servlet classes and if you don't provide the good libraries, it will fail at runtime. – Guillaume F. Dec 30 '16 at 00:23
  • @GuillaumeF. the problem I am facing is the servlet-api is coming from javeee-api-7.0.jar which is coming from my ORM dependency (Kundera). javeee-api-7.0.jar contains all the J2EE related jars which is required for the ORM so I just need to exclude servlet-api from that jar. Can you advice me the best way to remove that? – Raj Jan 05 '17 at 00:37

2 Answers2

0

add javeee-api-7.0.jar,it works for me.

南无增
  • 61
  • 6
0

We encountered this case in our project that contained loads of legacy code that was obviously not really well maintained.

We solved it by analyzing all dependencies inside all pom.xml files. For this issue particularly, what we did was to exclude javax.servlet api because there was a conflicting version that loaded and caused the cast error.

If you use eclipse, when opening the pom.xml files, you can check the dependencies with the Dependency Hierarchy tab at the bottom. There you can use the filter to see all servlet jars versions, and the top-level dependency that uses them. You can then exclude the problematic ones by right clicking on them and selecting "Exclude Maven Artifact..." And then the obvious: save your pom.xml, eventually repeat for other projects/modules, update your maven project configuration and finally build/install/run to see if the problem is fixed.

Hope that helps!

coder
  • 8,346
  • 16
  • 39
  • 53