4

What could cause a ClassNotFoundException for a Servlet Filter other than the obvious misconfiguration issues?

I have defined the servlet filter as such in my 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 have made sure it is included in a JAR file and that the JAR file is included in WEB-INF/lib directory, and that the class is also included in WEB-INF/classes directory.

But yet, I keep 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 are 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 are 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
jrobertsz66
  • 953
  • 1
  • 12
  • 29
  • did you check that your filter class is in the right package: com.ge.de.security ? – Guy Bouallet May 08 '15 at 22:10
  • You still can edit your question to include complements... – Guy Bouallet May 08 '15 at 22:22
  • I don't think that Spring prevents you from declaring a standard filter. Double check your configuration and startup logs. There should be something you missed. – Guy Bouallet May 08 '15 at 22:25
  • Hmmm..I've been looking at this for 1/2 day and I don't see it. Everything seems correct to me. BTW - it is not the first time that I create a filter also, I've been using filters for several years. I renamed everything as com.foo.security package and edited the original post with it. – jrobertsz66 May 08 '15 at 22:35
  • Check this link might help, http://stackoverflow.com/questions/11659047/servlet-web-xml-filter-class-not-found-exception – Ashish Yete May 08 '15 at 22:44
  • Thanks, I checked that post but I don't see anything that would resolve my issue. The Servlet Filter I am using implements the javax.servlet.Filter class already. – jrobertsz66 May 08 '15 at 22:51
  • I think I may need to deploy this application on Windows and use FileMon to see where in the heck Tomcat is looking for the class. – jrobertsz66 May 08 '15 at 22:52
  • Hmm...I added a which.jsp page to tell me where classes are being loaded from and then tried to invoke it and I got a 404 not found from DispatchServlet in the Spring framework. I think Spring is causing side effects. – jrobertsz66 May 08 '15 at 23:07
  • so `com.foo.security` is in the `WEB-INF/classes` **and** in a jar file under `WEB-INF/lib`? Remove one of them... – Elliott Frisch May 08 '15 at 23:15
  • I have. And the problem still persists. The application is using jboss 4.2.3 GA and I am starting to believe that it may be a bug in that version because, if I comment out the filter in the web.xml file and redeploy, it actually deploys fine, and then I can invoke a page called which.jsp page, which tells me that com.foo.security.SecurityFilter is being loaded from the WEB-INF/classes directory of the deployed war file under the tmp directory of JBOSS. But if a uncomment the filter in the web.xml file, I am then back to square one. – jrobertsz66 May 08 '15 at 23:53
  • Just as a test, I created another web application, copied the same filter definition to the new web.xml file, and copied the same SecurityFilter.java class to the new web application and deployed it under Glassfish instead of JBOSS, and it deploys fine and runs fine and I can see the System.out.println statements from the Servlet filter. – jrobertsz66 May 09 '15 at 00:57
  • So that means, that it is a JBoss issue and nothing wrong with my settings and configuration. – jrobertsz66 May 09 '15 at 00:58
  • And just to prove it, I took the new application that runs fine in glassfish and tried to deploy it on JBoss 4.2.3 GA, and got the same error again: Exception starting filter SecurityFilter. ClassNotFoundException: com.foo.security.SecurityFilter – jrobertsz66 May 09 '15 at 01:06
  • It's a Servlet version issue, [JBoss 4.2.3 supports Servlet 2.5 / JSP 2.1](http://stackoverflow.com/a/2363723/2970947). – Elliott Frisch May 09 '15 at 20:04
  • Hmm...so servlet version 2.5 doesn't support Servlet Filters? – jrobertsz66 Jul 31 '15 at 18:04
  • @Elliot Per this URL, Servlet Filters are supported since Servlet version 2.3 https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/Filter.html – jrobertsz66 Aug 10 '15 at 21:48
  • Did you find a resolution to this? I'm having a similar issue & im not sure where to look. – Mr Smith Jun 02 '16 at 20:14
  • Have you manage to solved this issue? I get the same issue for the past week now, very frustrating. – imperialx Oct 10 '16 at 08:18
  • @MrSmith have you solved this issue, I am also facing the same.. – Amogh Jan 05 '18 at 06:44
  • Same here... even worse, I have a servlet filter that works fine in one machine, but same code checked from same repo, same branch, same commit, fails on another machine – Alex R Jul 02 '19 at 20:37

1 Answers1

0

Here's the problem: the filter class is one of the very first external dependencies that get loaded when the application is starting up. So there is a broad range of possible causes that would manifest as "filter class not found" that are really just a regular class not found.

Here are a few things to check (hopefully others can add to this list over time):

Alex R
  • 11,364
  • 15
  • 100
  • 180