views:

63

answers:

2

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?

+1  A: 

@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.)

Stephen C
3 can be excluded since the error signals that it was in compiletime classpath, but not in the runtime classpath. 1 would have resulted in different problems than a simple `NoClassDefFoundError`. I am not sure of 4, this would require more knowledge of the developer and since this error is pretty trivial... Option 2 is a nice one, but if I am not wrong, this would rather have resulted in `NoClassDefFoundError` of the listener class' implementation, not the interface.
BalusC
wouldnt the ant build have logged some errors if the stuff i was compiling had issues? i guess ill look into verbose flags for the build process first
mkoryak
@mkoryak - yes, it should have. Check those build logs. Also, it may be helpful if you included full stacktraces instead of just error messages. Some significant details can only deduced from the full stacktraces.
Stephen C
your answer was correct. the problem ended up being the build.xml not doing what i thought it was doing. there were compilation errors in there and i didnt see them because clean was not performed correctly prior to build
mkoryak
+2  A: 

As per its javadoc that class was introduced in Servlet API version 2.3.

If you're receiving this error, then it can have basically three causes:

  1. Your web.xml is declared as Servlet 2.2 or lower (or incorrectly declared; Tomcat may fall back to least compatibility modus). Since you're using Java EE 5 and thus Servlet 2.5, the web.xml should then be declared like as:

    <web-app 
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="YourWebAppID" version="2.5">
    
  2. Your servletcontainer doesn't support Servlet 2.3 at all and will fall back to least compatibilty modus. But this can be excluded since Tomcat 6 should support Servlet 2.5.

  3. You actually have another Servlet API JAR file of an ancient version in the classpath which is taking precedence in classloading. Since you already excluded WEB-INF/lib the next places to look would be JRE/lib and JRE/lib/ext folders.


Update: as per your edit, FilterChain was also introduced in Servlet API version 2.3.

1 + 1 = ... :)

BalusC
thanks for your answer. i remember you from sun forums. ill check out your suggestions - hopefully something there helps.
mkoryak