Tomcat 侦听器的 ClassNotFoundException

Tomcat ClassNotFoundException for Listener

我在包 nl.hu.tho6.listener 中有一个名为 ContextListener 的侦听器。部署 tomcat 时会出现 SEVERE: Error listenerStart 错误。在 localhost 日志中四处挖掘,我发现 tomcat 为侦听器 class.

抛出 ClassNotFoundException

web.xml 看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <listener>
        <listener-class>nl.hu.tho6.listener.ContextListener</listener-class>
    </listener>

</web-app> 

我完全不知道出了什么问题。我很明显 tomcat 找不到 class 但映射是正确的。这可能与我正在使用 IntelliJ 这一事实有关吗?

编辑:

整个异常:

jan 22, 2015 1:51:37 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class nl.hu.tho6.listener.ContextListener
java.lang.ClassNotFoundException: nl.hu.tho6.listener.ContextListener
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:506)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:488)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:115)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4909)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.manager.ManagerServlet.start(ManagerServlet.java:1286)
    at org.apache.catalina.manager.HTMLManagerServlet.start(HTMLManagerServlet.java:694)
    at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:217)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:213)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:612)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

我希望错误消息是正确的,除非您有理由不相信?

我会查看 war 文件并检查 class 是否存在,例如:

jar -tf mywebapp.war | grep ContextListener

如果未显示,请检查您的构建过程 - 您没有提到您使用的是 ant、maven 还是其他工具,但这就是开始的地方。

根据评论,文件位于错误的文件夹中。 Class 应该在 war/WEB-INF/classes/nl/hu/tho6/listener/ 不在 war/WEB-INF/classes/production/THO6/nl/hu/tho6/listener/