java.lang.NoClassDefFoundError: HttpSessionListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3902705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:55:10  来源:igfitidea点击:

java.lang.NoClassDefFoundError: HttpSessionListener

javatomcatjava-ee-5

提问by mkoryak

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

我正在尝试部署一场我没有编写的War,但在我的日志中出现此错误:

java.lang.NoClassDefFoundError: HttpSessionListener

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

我知道 HttpSessionListener 位于servlet-api.jar 中,它位于 tomcat(我的应用程序服务器)的 lib 目录中。

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

我尝试在 war 的 WEB-INF/lib 文件夹中包含 servlet-api.jar,但是日志因为这样做而对我大喊大叫:

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.

互联网声称您不必在 lib 文件夹中包含该类。

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:

编辑:我从 web.xml 中删除了有问题的侦听器(导致上述问题),因为它看起来并不重要。这揭示了更多错误:

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?

我错过了什么?

回答by Stephen C

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

@BalusC 的解释听起来比我的更合理......

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 HttpSessionListenerto fail with an uncaught exception during static initialization. (This is kind of implausible, since HttpSessionListeneris 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.HttpSessionListenerrather 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 HttpSessionListenerclass is not on the classloader's effective classpath.

  1. servlet-api.jar 不在 $CATALINA_HOME/lib 中,或者由于某种原因不包含该类。(我知道你说你“知道”它在那里,但你没有特别说你检查过它。)

  2. 其他东西被破坏,导致第一次尝试加载HttpSessionListener失败,并在静态初始化期间出现未捕获的异常。(这有点令人难以置信,因为它HttpSessionListener是一个接口。但值得检查日志以查找早期的类加载错误......以防万一。)

  3. 丢失的类可能被命名foo.bar.HttpSessionListener而不是javax.servlet.http.HttpSessionListener. 这很可能会出现在嵌套的堆栈跟踪中。

  4. 如果您正在部署的 WAR 中的某些内容正在创建自己的类加载器,则可能是这样做不正确并且HttpSessionListener该类不在类加载器的有效类路径上。

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!

如果您现在看到日志中报告了未解决的编译错误,您应该怀疑 WAR 文件和用于构建它的过程。具体来说,听起来 WAR 包括具有 Java 编译错误的类!

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

(或者这可能是编译 JSP 的问题……但这也会出现在日志中。)

回答by BalusC

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

根据其 javadoc,该类是在 Servlet API 2.3 版中引入的。

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

如果您收到此错误,则基本上可能有以下三个原因:

  1. Your web.xmlis 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.xmlshould 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/libthe next places to look would be JRE/liband JRE/lib/extfolders.

  1. web.xml声明为 Servlet 2.2 或更低版本(或声明不正确;Tomcat 可能会退回到最低兼容性模式)。由于您使用的是 Java EE 5 和 Servlet 2.5,因此web.xml应将其声明为:

    <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. 您的 servletcontainer 根本不支持 Servlet 2.3,并且会退回到最低兼容性模式。但这可以排除,因为 Tomcat 6 应该支持 Servlet 2.5。

  3. 您实际上在类路径中有另一个旧版本的 Servlet API JAR 文件,它优先于类加载。由于您已经排除WEB-INF/lib了下一个要查找的位置JRE/libJRE/lib/ext文件夹。



Update: as per your edit, FilterChainwas alsointroduced in Servlet API version 2.3.

更新:根据您的编辑,FilterChain于Servlet API的2.3版推出。

1 + 1 = ... :)

1 + 1 = ... :)

回答by SuBZ

java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener for this NoClassDefFoundError on HttpSessionListener , ServletListener , ServletContextListener, etc. can be caused by a custom classloader like Sysdeo DevLoader (when using it with Eclipse) in you Context definition in the Tomcat's server.xml file.

java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener for this NoClassDefFoundError on HttpSessionListener , ServletListener , ServletContextListener 等可能是由自定义类加载器引起的,如 Sysdeo DevLoader(与 Eclipse 一起使用时)在 Tomcat 的 server.xml 中的上下文定义文件。

<Loader classname="org.apache.catalina.loader.DevLoader"
        reloadable="true" debug="1" />

there.....insted of this use...

那里......开始使用......

<Loader classname="org.apache.catalina.loader.DevLoader" 
        reloadable="true"
        debug="1" useSystemClassLoaderAsParent="false"/>

and add DevLoader.jar to ur class path

并将 DevLoader.jar 添加到您的类路径