我正在做一个Maven项目。已经在tomcat7中创建了一个侦听器类,但是当我部署时,出现以下错误:

侦听器类

public class DBInitialisationListener implements ServletContextListener {

public void contextInitialized(ServletContextEvent event) {

    System.out.println("Db Initialised");
}

public void contextDestroyed(ServletContextEvent event) {
    DatabaseFactory.close();
    System.out.println("Db Closed");
}
}


web.xml

<listener>
        <listener-class>com/nrift/ovmgmt/core/DBInitialisationListener</listener-class>
</listener>


我的错误日志:

Sep 04, 2014 2:14:31 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class com/nrift/ovmgmt/core/DBInitialisationListener
java.lang.NoClassDefFoundError: IllegalName: com/nrift/ovmgmt/core/DBInitialisationListener
    at java.lang.ClassLoader.preDefineClass(ClassLoader.java:655)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:794)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2888)
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1172)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:137)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4733)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:977)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1655)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

最佳答案

您必须用点“。”表示包装类为定义的。

<listener>
        <listener-class>com.nrift.ovmgmt.core.DBInitialisationListener</listener-class>
</listener>


这是因为Maven找不到课程

要检查是否已在<listener-class>标记中正确添加了该类,请尝试在该类上添加一个CTRL+Click

08-06 17:23