1

I'm trying to use Guice 4.0 for web applications with Tomcat 8. I tried to do example from wiki but the I get this error in Tomcat Localhost log:

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

This is my web.xml file:

<?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>

And my config file for 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: This is my Maven pom.xml file

    <?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>
  • How did you solve this? I don't see the duplicate that is marked here... – Roel Jun 09 '16 at 13:28
  • In my opinion this is not a duplicate of a much more generic class not found exception answer. Details for how to exactly set up the mentioned GuiceServletContextListener can be found [here](http://www.aberger.at/en/blog/implementation/java/2016/11/16/google-app-engine-project.html) – Christian Aberger Nov 20 '16 at 13:42

1 Answers1

2

It sounds like your war doesn't include the Guice servlet jar. If you're using maven to build the war then you may need to add a dependency on guice-servlet in your pom...

<dependency>
    <groupId>com.google.inject.extensions</groupId>
    <artifactId>guice-servlet</artifactId>
    <version>4.0</version>
</dependency>
sisyphus
  • 6,174
  • 1
  • 25
  • 35
  • No, I added it in pom, as it was instructed. Added my pom.xml content to question. – Rustem Shaidullin Nov 28 '15 at 15:38
  • NoClassDefFoundException is caused when Tomcat is not able to find the path for the file that is being called. Please make sure you added your all jars in $Tomcat_Home/your-project-directory/WEB-INF/lib folder and all java files are included in web.xml as well as java class resides in $Tomcat_Home/your-project-directory/WEB-INF/classes folder – Ghayel Nov 28 '15 at 17:37