restjersey-2.0embedded-jetty

Strange exception using Jersey with embedded Jetty


I have created an embedded Jetty application which utilizes Jersey in order to implement several RESTful services. I am using some standard code as described here in Stack Overflow as well as other websites:

    public static void main(String[] args)
    {
       Server server = new Server(8080);
      
       ServletContextHandler ctx = new ServletContextHandler(
                                      ServletContextHandler.SESSIONS);
      
       ctx.setContextPath("/");
      
       ServletHolder holder = ctx.addServlet(
                 "org.glassfish.jersey.servlet.ServletContainer.class", "/*");
       holder.setInitOrder(0);
      
       holder.setInitParameter("jersey.config.server.provider.classnames",
                              RestfulClass.class.getCanonicalName());
      
       server.setHandler(ctx);
      
       try
       {
          server.start();
          server.join();
       }
       catch(Exception exe)
       {
          exe.printStackTrace();
       }
    }

I've used all the recommended jar files, and several other jar files that the various blogs and sites failed to mention. When running the Jetty application, I get the following exception:

java.lang.ClassNotFoundException:    org.glassfish.jersey.servlet.ServletContainer.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.eclipse.jetty.util.Loader.loadClass(Loader.java:86)
at org.eclipse.jetty.servlet.BaseHolder.doStart(BaseHolder.java:95)

<several lines omitted for brevty>

Caused by: javax.servlet.UnavailableException: org.glassfish.jersey.servlet.ServletContainer.class
at org.eclipse.jetty.servlet.BaseHolder.doStart(BaseHolder.java:102)
at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:361)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:874)
... 11 more

It is the "UnavailableException" that I do not understand. The ServletContainer class is actually in one of the Jar files (in jersey-container-servlet-core.jar, to be precise), but for some reason it is identified as "unavailable". This is causing a class that is actually in a referenced Jar file to be "not found"!

Can anyone tell me what is causing this UnavailableException and (more importantly) how I can prevent it from being thrown?

Someone please advise...


Solution

  • ServletHolder holder = ctx.addServlet(
             "org.glassfish.jersey.servlet.ServletContainer.class", "/*");
    

    You are using a String for the class name. When doing this, you don't use the .class suffix. That at only when you want to get the actual Class object. You have two options

    1. Remove the .class from the String

      ServletHolder holder = ctx.addServlet(
           "org.glassfish.jersey.servlet.ServletContainer", "/*");
      
    2. Remove the "" (double quotes) and just use the Class object1.

      ServletHolder holder = ctx.addServlet(
           org.glassfish.jersey.servlet.ServletContainer.class, "/*");
      

    1 - See addServlet(Class, String)