java.lang.NoClassDefFoundError : com/google/inject/servlet/GuiceServletContextListener 错误

标签 java tomcat servlets guice

<分区>

我正在尝试将 Guice 4.0 用于 Tomcat 8 的 Web 应用程序。我尝试从 wiki 做示例,但我在 Tomcat Localhost 日志中收到此错误:

    28-Nov-2015 16:36:26.136 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.listenerStart Error configuring application listener of class com.nirus.guice_confige.MyGuiceServletConfig
 java.lang.NoClassDefFoundError: com/google/inject/servlet/GuiceServletContextListener
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2496)
    ...

这是我的 web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <filter>
        <filter-name>guiceFilter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>guiceFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <listener>
        <listener-class>com.nirus.guice_confige.MyGuiceServletConfig</listener-class>
    </listener>

</web-app>

还有我的 Guice 配置文件:

public class MyGuiceServletConfig extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new ServletModule(){
            @Override
            protected void configureServlets() {
                serve("/JoinLobby").with(JoinLobby.class);
                serve("/UpdateLobby").with(UpdateLobby.class);
                bind(ILobby.class).to(Lobby.class);
            }
        });
    }
}

UPD1: 这是我的 Maven pom.xml 文件

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>NiRus</groupId>
    <artifactId>ImagServerApp</artifactId>
    <version>0.0.1-FirstStep</version>

    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>4.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-servlet</artifactId>
            <version>4.0</version>
        </dependency>
    </dependencies>
</project>

最佳答案

听起来您的 war 不包括 Guice servlet jar。如果您使用 Maven 构建 war ,那么您可能需要在您的 pom 中添加对 guice-servlet 的依赖...

<dependency>
    <groupId>com.google.inject.extensions</groupId>
    <artifactId>guice-servlet</artifactId>
    <version>4.0</version>
</dependency>

关于java.lang.NoClassDefFoundError : com/google/inject/servlet/GuiceServletContextListener 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33972097/

相关文章:

java - 获取zxing生成的图片路径

java - 从 tomcat context.xml 而不是 application.properties 获取数据源

java - 多个 Spring DispatcherServlet 处理不同的 url

java - NPE 未发生或未被 Servlet 捕获

Java 1.6内存泄漏: HashMap and ArrayList

java - 在 Java 中指示代码是从 IntelliJ/Eclipse 等还是命令行运行

java - @Override,在接口(interface)中使用默认方法

java - 在 java 中检索客户端 IP 地址时获取 request.getHeader ("X-FORWARDED-FOR"的空值?

java - Java/Tomcat 服务器的最佳 Comet/Push 技术?

session - Captcha Servlet 导致 java.lang.IllegalStateException : PWC3999: Cannot create a session after the response has been committed