What can cause a ClassNotFoundException for a servlet filter other than the obvious misconfiguration issues?
What can cause a ClassNotFoundException exception for the filter servlet other than the obvious misconfiguration issues?
I have defined the servlet filter as such in the web.xml file:
<filter>
<filter-name>SecurityFilter</filter-name>
<filter-class>com.foo.security.SecurityFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SecurityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
And I have defined the servlet filter as such in my class:
package com.foo.security;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
public class SecurityFilter implements Filter{
private static final int MAX_LENGTH = 4096;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Initializing SecurityFilter!!!");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try{
System.out.println("Calling SecurityFilter.doFilter()!!!");
chain.doFilter(request, response);
}catch(Exception e){
}finally{
}
}
@Override
public void destroy() {
System.out.println("Destroying SecurityFilter!!!");
}
}
And I made sure that it is included in the JAR file and that the JAR file is included in the WEB-INF / lib directory and that the class is also included in the WEB-INF / classes directory.
But still I am getting this exception:
2015-05-06 13:01:42.926 ERROR [ScannerThread] org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/testApp] - Exception starting filter SecurityFilter
java.lang.ClassNotFoundException: com.foo.security.SecurityFilter
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:249)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3722)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4367)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:790)
at org.apache.catalina.core.ContainerBase.access$000(ContainerBase.java:122)
at org.apache.catalina.core.ContainerBase$PrivilegedAddChild.run(ContainerBase.java:144)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:768)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:553)
at sun.reflect.GeneratedMethodAccessor512.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
Here is the contents of the JAR file:
META-INF/
META-INF/MANIFEST.MF
com/
com/foo/
com/foo/security/
com/foo/security/SecurityFilter.class
And here is the contents of the WAR file:
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/classes/
WEB-INF/classes/com/
WEB-INF/classes/com/foo/
WEB-INF/classes/com/foo/security/
WEB-INF/lib/
WEB-INF/classes/com/foo/security/SecurityFilter.class
WEB-INF/jboss-web.xml
WEB-INF/lib/com.foo.security.jar
WEB-INF/web.xml
source to share
Here's the problem: the filter class is one of the very first external dependencies that are loaded when the application starts. Thus, there is a wide range of possible causes, which can manifest themselves as "filter class not found", which are in fact a normal class not found.
Here are a few things to check (hopefully others will eventually be able to add to this list):
- Poor communication between Maven / Eclipse / Tomcat: Tomcat throws ClassNotFound exceptions for classes in other open eclipse projects
source to share