Search code examples
javatomcatjava-ee-5

java.lang.NoClassDefFoundError: HttpSessionListener


I am trying to deploy a war that i didnt write and i am getting this error in my logs:

java.lang.NoClassDefFoundError: HttpSessionListener

i know that HttpSessionListener lives in servlet-api.jar which is found in the lib dir of tomcat(my app server).

I tried including servlet-api.jar in the war's WEB-INF/lib folder, but the logs yelled at me for doing that:

INFO: validateJarFile(/home/test/apache-tomcat-6.0.18/webapps/test/WEB-INF/lib/servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class

the internets claim that you dont have to include that class in your lib folder.

edit: i removed the offending listener (which was causing the problem above) from web.xml because it didnt look very important. this revealed more errors:

java.lang.Error: Unresolved compilation problem: 
    The type javax.servlet.FilterChain cannot be resolved. It is indirectly referenced from required .class files

what am i missing?


Solution

  • @BalusC's explanation sounds more plausible than mine ...

    Some other possible explanations / things to check:

    1. The servlet-api.jar is not in $CATALINA_HOME/lib, or for some reason doesn't contain the class. (I know you said you "know" it's there, but you didn't specifically say you checked it.)

    2. Something else is broken which caused the first attempted load of HttpSessionListener to fail with an uncaught exception during static initialization. (This is kind of implausible, since HttpSessionListener is an interface. But it is worth checking the logs for earlier class loading errors ... just in case.)

    3. The missing class might be named foo.bar.HttpSessionListener rather than javax.servlet.http.HttpSessionListener. This is likely to show up in the nested stack trace.

    4. If something in the WAR you are deploying is creating its own classloader, it is possible that is is doing this incorrectly and the HttpSessionListener class is not on the classloader's effective classpath.

    EDIT

    If you are now seeing unresolved compilation errors reported in the logs, you should be suspecting the WAR file and the process used to build it. Specifically, it sounds like the WAR includes classes that had Java compilation errors!

    (Or maybe this is a problem compiling JSPs ... but that would show up in the logs too.)