title: Guice 4.0 Jersey 2.22.1 - ServiceLocatorImpl has been shut down
body: <p>I am trying to get a Jersey 2 web app running with Guice 4 for dependency injection and Maven. I also use <a href="https://github.com/Squarespace/jersey2-guice" rel="nofollow">https://github.com/Squarespace/jersey2-guice</a>.</p>
  
  <p>The WAR-file builds fine, but when trying to deploy it on a Jetty web server I get the following stack trace:</p>
  
  <pre><code>[2016-01-09 09:13:24,581] Artifact packageapi:war: Artifact is being deployed, please wait...
  2016-01-09 21:13:49.760:WARN:/.0-SNAPSHOT:Scanner-1: unavailable
  java.lang.IllegalStateException: ServiceLocatorImpl(GuiceServiceLocator-1,0,1115440386) has been shut down
      at org.jvnet.hk2.internal.ServiceLocatorImpl.checkState(ServiceLocatorImpl.java:2270)
      at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:737)
      at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:714)
      at org.glassfish.hk2.utilities.ServiceLocatorUtilities.enablePerThreadScope(ServiceLocatorUtilities.java:106)
      at org.glassfish.jersey.internal.inject.Injections._createLocator(Injections.java:141)
      at org.glassfish.jersey.internal.inject.Injections.createLocator(Injections.java:123)
      at org.glassfish.jersey.server.ApplicationHandler.&lt;init&gt;(ApplicationHandler.java:330)
      at org.glassfish.jersey.servlet.WebComponent.&lt;init&gt;(WebComponent.java:390)
      at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:172)
      at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:364)
      at javax.servlet.GenericServlet.init(GenericServlet.java:244)
      at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:626)
      at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:405)
      at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:875)
      at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:346)
      at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1380)
      at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1342)
      at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:772)
      at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:259)
      at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:518)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
      at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:41)
      at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:188)
      at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:499)
      at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:147)
      at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:180)
      at org.eclipse.jetty.deploy.providers.WebAppProvider.fileAdded(WebAppProvider.java:459)
      at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:64)
      at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:610)
      at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:529)
      at org.eclipse.jetty.util.Scanner.scan(Scanner.java:392)
      at org.eclipse.jetty.util.Scanner$1.run(Scanner.java:329)
      at java.util.TimerThread.mainLoop(Timer.java:555)
      at java.util.TimerThread.run(Timer.java:505)
  2016-01-09 21:13:49.761:WARN:oejw.WebAppContext:Scanner-1: Failed startup of context o.e.j.w.WebAppContext@5066ebe1{/package-1.0-SNAPSHOT,file:///home/my/Dropbox/Programming/Java/packageapi/target/package-1.0-SNAPSHOT/,STARTING}{/home/my/Dropbox/Programming/Java/packageapi/target/package-1.0-SNAPSHOT.war}
  javax.servlet.ServletException: Jersey Web Application@ef2c8cb4==org.glassfish.jersey.servlet.ServletContainer,1,false
      at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:647)
      at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:405)
      at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:875)
      at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:346)
      at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1380)
      at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1342)
      at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:772)
      at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:259)
      at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:518)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
      at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:41)
      at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:188)
      at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:499)
      at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:147)
      at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:180)
      at org.eclipse.jetty.deploy.providers.WebAppProvider.fileAdded(WebAppProvider.java:459)
      at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:64)
      at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:610)
      at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:529)
      at org.eclipse.jetty.util.Scanner.scan(Scanner.java:392)
      at org.eclipse.jetty.util.Scanner$1.run(Scanner.java:329)
      at java.util.TimerThread.mainLoop(Timer.java:555)
      at java.util.TimerThread.run(Timer.java:505)
  Caused by: 
  java.lang.IllegalStateException: ServiceLocatorImpl(GuiceServiceLocator-1,0,1115440386) has been shut down
      at org.jvnet.hk2.internal.ServiceLocatorImpl.checkState(ServiceLocatorImpl.java:2270)
      at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:737)
      at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:714)
      at org.glassfish.hk2.utilities.ServiceLocatorUtilities.enablePerThreadScope(ServiceLocatorUtilities.java:106)
      at org.glassfish.jersey.internal.inject.Injections._createLocator(Injections.java:141)
      at org.glassfish.jersey.internal.inject.Injections.createLocator(Injections.java:123)
      at org.glassfish.jersey.server.ApplicationHandler.&lt;init&gt;(ApplicationHandler.java:330)
      at org.glassfish.jersey.servlet.WebComponent.&lt;init&gt;(WebComponent.java:390)
      at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:172)
      at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:364)
      at javax.servlet.GenericServlet.init(GenericServlet.java:244)
      at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:626)
      at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:405)
      at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:875)
      at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:346)
      at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1380)
      at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1342)
      at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:772)
      at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:259)
      at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:518)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
      at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:41)
      at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:188)
      at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:499)
      at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:147)
      at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:180)
      at org.eclipse.jetty.deploy.providers.WebAppProvider.fileAdded(WebAppProvider.java:459)
      at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:64)
      at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:610)
      at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:529)
      at org.eclipse.jetty.util.Scanner.scan(Scanner.java:392)
      at org.eclipse.jetty.util.Scanner$1.run(Scanner.java:329)
      at java.util.TimerThread.mainLoop(Timer.java:555)
      at java.util.TimerThread.run(Timer.java:505)
  </code></pre>
  
  <p>My pom.xml:</p>
  
  <pre><code>&lt;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/maven-v4_0_0.xsd"&gt;
  
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;my.package&lt;/groupId&gt;
      &lt;artifactId&gt;package&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;package&lt;/name&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
  
      &lt;dependencyManagement&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.glassfish.jersey&lt;/groupId&gt;
                  &lt;artifactId&gt;jersey-bom&lt;/artifactId&gt;
                  &lt;version&gt;${jersey.version}&lt;/version&gt;
                  &lt;type&gt;pom&lt;/type&gt;
                  &lt;scope&gt;import&lt;/scope&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
      &lt;/dependencyManagement&gt;
  
      &lt;dependencies&gt;
           &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-media-moxy&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.9&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.google.inject&lt;/groupId&gt;
              &lt;artifactId&gt;guice&lt;/artifactId&gt;
              &lt;version&gt;4.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.squarespace.jersey2-guice&lt;/groupId&gt;
              &lt;artifactId&gt;jersey2-guice&lt;/artifactId&gt;
              &lt;version&gt;0.10&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
              &lt;version&gt;3.1.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-simple&lt;/artifactId&gt;
              &lt;version&gt;1.7.13&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.5.1&lt;/version&gt;
                  &lt;inherited&gt;true&lt;/inherited&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.8&lt;/source&gt;
                      &lt;target&gt;1.8&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
      &lt;properties&gt;
          &lt;jersey.version&gt;2.22.1&lt;/jersey.version&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;/properties&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>The web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;Jersey Web Application&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
              &lt;param-value&gt;my.package&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Jersey Web Application&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;my.package.guice.ApiServletContextListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;filter&gt;
          &lt;filter-name&gt;guiceFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;com.google.inject.servlet.GuiceFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;guiceFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>This is the JerseyGuiceServletContextListener:</p>
  
  <pre><code>package my.package.guice;
  
  import com.google.inject.Module;
  import com.squarespace.jersey2.guice.JerseyGuiceServletContextListener;
  
  import java.util.Collections;
  import java.util.List;
  
  public class ApiServletContextListener extends JerseyGuiceServletContextListener {
      private static final ApiModule apiModule = new ApiModule();
      @Override
      protected List&lt;? extends Module&gt; modules() {
          return Collections.singletonList(apiModule);
      }
  }
  </code></pre>
  
  <p>The test resource:</p>
  
  <pre><code>package my.package;
  
  import javax.inject.Inject;
  import javax.inject.Named;
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.MediaType;
  
  @Path("test")
  public class TestResource {
  
      @Inject
      @Named("test")
      private String s;
  
      public TestResource() {
          System.out.println("LOLOLOLOL");
      }
  
      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public String getIt() {
          System.out.println("LOLOLOL");
          return s;
      }
  }
  </code></pre>
  
  <p>The GuiceModule:</p>
  
  <pre><code>package my.package.guice;
  
  import my.package.TestResource;
  import com.google.inject.AbstractModule;
  import com.google.inject.name.Names;
  
  public class ApiModule extends AbstractModule {
      @Override
      protected void configure() {
          bind(TestResource.class);
  
          bind(String.class)
                  .annotatedWith(Names.named("test"))
                  .toInstance("It works!");
      }
  }
  </code></pre>
  
  <p>Switching to Guice 3.0 does not solve the issue.
  How can I fix this?</p>