Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError: javax/servlet/Filter

I am getting this error when i start tomcat

java.lang.NoClassDefFoundError: javax/servlet/Filter
at java.lang.ClassLoader.findBootstrapClass(Native Method)
at java.lang.ClassLoader.findBootstrapClass0(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at      org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1301)
at    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1232)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:207)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:302)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:78)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3666)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4258)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)

i have already put the servlet.jar in classpath and tomcat_home/commons/lib folder

please can any one suggest what to do.

like image 519
prem Avatar asked Jan 13 '12 19:01

prem


3 Answers

java.lang.NoClassDefFoundError: javax/servlet/Filter

This particular exception can occur when you have got a copy of the servlet.jar which originated from an ancient Servlet 2.2 container or older and that file got precedence in classloading over Tomcat's own libraries. The Filter class was namely introduced in Servlet 2.3.


i have already put the servlet.jar in classpath and tomcat_home/commons/lib folder

You should not do that. Remove all servletcontainer-specific libraries from your /WEB-INF/lib, the "classpath" and the Tomcat's /common/lib folders.

The Tomcat servletcontainer already ships with the right APIs in its own /lib folder. You should not have any copy in your webapp's /WEB-INF/lib, nor elsewhere in the classpath, also not the ones from a completely different servletcontainer.

This common beginner's mistake of attempting to download/copy random servletcontainer specific libraries is commonly caused by the inability to compile servlet classes by javac or in the IDE. When using javac, you need to reference Tomcat's /lib in the -cp argument. When using an IDE, you need to reference Tomcat as Targeted runtime in project's properties.

See also:

  • How do I import the javax.servlet API in my Eclipse project?
like image 78
BalusC Avatar answered Nov 15 '22 08:11

BalusC


add this to your pom.xml, replace the version with any latest stable version. search for spring-web in mvnrepository.com and get the latest version

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.2.0.RELEASE</version>
</dependency>
like image 37
Mohammed Rafeeq Avatar answered Nov 15 '22 09:11

Mohammed Rafeeq


Unable to find the jar version of servlet jar that was causing issue, I explicitly added correct version of jar to Tomcat classpath :

enter image description here

This jar resides in %TOMCAT_HOME%/lib

Note : ensure servlet-api jar appears first.

like image 45
blue-sky Avatar answered Nov 15 '22 08:11

blue-sky