Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] embedded jetty and onejar plugin


I am trying to create an application using embedded Jetty (version 7.4.0.v20110414) and want to package it using the onejar maven plugin (

Here is the code where I start jetty:

    Server server = new Server(8180);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setWelcomeFiles(new String[]{"index.html"});

    ServletHolder servletHolder = new ServletHolder(new DefaultServlet());
    URL resource = getClass().getResource("/web/");
    String resourceBase = resource.toURI().toString();
    servletHolder.setInitParameter("resourceBase", resourceBase);
    context.addServlet(servletHolder, "/");


When I do java -jar onejar.jar and go to http://localhost:8180/index.html I get a 404.

My onejar.jar looks like this:


(Rest omitted for readability).

I debugged through the application and noticed it throws (and ignores) an exception in the org.eclipse.jetty.util.resource.JarFileResource class in the method exists() in this piece of code:

                    JarURLConnection c=(JarURLConnection)((new URL(_jarUrl)).openConnection());
                catch(Exception e)

The _jarUrl contains: jar:file:/target/onejar.jar!/main/myapp.jar!/web/index.html and throws a ZipException when doing c.getJarFile(): error in opening zip file.

Does anyone has experience in using embedded jetty with the mentioned onejar plugin? What I'd like to do is do java -jar myjar.jar where myjar contains everything that is needed, no additional files and such.

Extra information:

I also created a unit test for this asfollows:

  public void defaultServlet() throws Exception {
    final Server server = new Server(8181);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setWelcomeFiles(new String[]{"index.html"});
    DefaultServlet defaultServlet = new DefaultServlet();
    ServletHolder servlet = new ServletHolder(defaultServlet);
    servlet.setInitParameter("resourceBase", "jar:file:/target/onejar.jar!/main/myapp.jar!/web/");
    context.addServlet(servlet, "/");
    Resource resource = defaultServlet.getResource("/index.html");

This tests succeeds, meaning resource is not null. Does a real request use something else to find the resources that the getResource method on the servlet?

Thanks in advance,


Back to the top