7

im trying to develop an web server by embedding jetty. So with jetty 7.3 everything worked fine. Yesterday I updated the jetty libaries to the newest version 8.0.3 and now I get an Exception by creating a ServletContextHandler.

Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/FilterRegistration at org.eclipse.jetty.servlet.ServletContextHandler.(ServletContextHandler.java:126) at org.eclipse.jetty.servlet.ServletContextHandler.(ServletContextHandler.java:106) at org.eclipse.jetty.servlet.ServletContextHandler.(ServletContextHandler.java:94) at org.gemsjax.server.GemsJaxServer.main(GemsJaxServer.java:38)

So what I do is:

    public static void main(String[] args) {

     Server server = new Server(8080);


        ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
        servletContext.setContextPath("/servlets");
        servletContext.addServlet(new ServletHolder( new CollaborationWebSocketServlet()),"/collaboration");


        // The ResourceHandler to handle static web content
        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirectoriesListed(true);
        resourceHandler.setWelcomeFiles(new String[]{ "index.html" });


        resourceHandler.setResourceBase("./war/");


        ContextHandler resourceContext = new ContextHandler();
        resourceContext.setContextPath("/static");
        resourceContext.setHandler(resourceHandler);



        HandlerCollection handlers = new HandlerCollection();


        handlers.addHandler(resourceContext);
        handlers.addHandler(servletContext);

        server.setHandler(handlers);

        try {
            server.start();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }


}

So the line that throw the exception is:

ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS);

Im using ubuntu 11.04 with:

openjdk java version "1.6.0_22" OpenJDK Runtime Environment (IcedTea6 1.10.2) (6b22-1.10.2-0ubuntu1~11.04.1) OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)

Does anyone have a suggestion?

sockeqwe
  • 15,574
  • 24
  • 88
  • 144

2 Answers2

10

The javax.servlet.FilterRegistration class was introduced in Servlet 3.0. This exception suggests that you have still libraries of an older Servlet API version in your runtime classpath which got precedence in classloading. For example a randomly from the Internet picked servlet-api.jar file in /WEB-INF/lib folder of your webapp or perhaps in the JRE's /lib folder. You should remove those servletcontainer-specific libraries which are sitting somewhere else in the classpath than in the target runtime itself.

If you did this to circumvent compilation problems, then you should instead have taken the target runtime's libraries in the classpath. In for example Eclipse, you can do it in Target Runtime section of project's properties. See also How do I import the javax.servlet API in my Eclipse project?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    thank you for your answere, but im still wondering where the Servlet 3.0 API is available to download? Does the Java SE JDK 1.6 update 22 (openjdk) not provide Servlet 3.0? Im confused ... – sockeqwe Oct 16 '11 at 16:54
  • 1
    You've already downloaded it. Jetty 8 is a Servlet 3.0 API implementation. You only need to grasp that the Java EE API's are **abstract** (i.e. they exist in paper only as specs and javadocs) and that the concrete Java EE / Servlet containers like Jetty, Tomcat, Glassfish, JBoss AS etc..etc.. are the **concrete** implementations (i.e. they contain the code and libraries). – BalusC Oct 16 '11 at 16:55
  • 1
    Ok, it was my error in reasoning. I have included all jetty libaries, but the exception is still there – sockeqwe Oct 16 '11 at 17:02
  • You're not the only and the first :) See also among others http://stackoverflow.com/questions/165230/is-the-offical-sun-java-ee-tutorial-the-best-way-to-learn-how-to-make-java-web-a/1876535#1876535 and http://stackoverflow.com/questions/7295096/what-exactly-is-java-ee/7295697#7295697. – BalusC Oct 16 '11 at 17:03
  • If you still have the exception then it means that you don't have included libraries of a Servlet 3.0 compatible implementation, or that you still have libraries of a older servlet version somewhere in the **runtime** classpath. Jetty 8 is (should be) Servlet 3.0 compatible (otherwise it would be a major bug in Jetty 8), so it's still the 2nd possible cause. Walk through all the paths covered by the runtime classpath once again to get rid of container-specific libraries of a different container like `servlet-api.jar`, `javaee.jar` and so on. Or maybe you've still Jetty 7 libs in the classpath? – BalusC Oct 16 '11 at 17:04
  • Im using GWT 2.3 to build the client, but i do no use GWT on server side (no RPC, no app engine etc.). Do you think that this could be the reason for my problem, since there is in the WEB-INF/lib folder the gwt-servelet.jar ? So what i want to develop is an Simple Client using GWT and an own (web)server by embedding jetty and to use WebSockets for the communication between server and client. – sockeqwe Oct 16 '11 at 17:19
  • I don't do GWT, but last what I know is that it doesn't support Servlet 3.0 yet, so that library could be very good the reason. Check it with a ZIP tool, you'll see that the class in question is missing in there (as all other Servlet 3.0 specific classes/methods). I also wonder if that library is necessary as you seem to not use GWT on server side, so you should be able to just remove it. – BalusC Oct 16 '11 at 17:27
  • hm, ok, thank you for your great help! I can't see any reason, why does GWT should have some impact on my own implemented server. My server is a "simple" java application and has nothing to do with GWT ... Maybe it would be easier if I use jetty 7 – sockeqwe Oct 16 '11 at 17:28
  • You could also just remove the offending library as you don't seem to use it. – BalusC Oct 16 '11 at 17:33
  • ok, i did it, but now i face another exception, that seems that is caused by other GWT dependancy to each other: java.lang.SecurityException: class "javax.servlet.FilterRegistration"'s signer information does not match signer information of other classes in the same package ... ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS); – sockeqwe Oct 16 '11 at 17:49
  • I'm afraid that there's no other way than to forget GWT or to fall back to Servlet 2.5 / Jetty 7 until they upgraded GWT to be Servlet 3.0 compatible. – BalusC Oct 16 '11 at 17:52
  • In my case, servlet-api:2.5 was inherited from hbase client. used "gradle dependencies" command to locate the same. – Rohit Verma Jul 25 '17 at 12:22
0

When you use SBT, FilterRegistration class is present in 3.0 and also if you use JETTY Or Java 8 this JAR 2.5 it automatically adds as dependency,

Fix: Servlet-api-2.5 JAR was the mess there, I resolved this issue by adding servlet-api-3.0 jar in dependencies,

enter image description here

Ravi Macha
  • 687
  • 8
  • 5