1

I'm trying to deploy a Jetty server from a jar file. When jar is being run on the server, it reaches the Jetty 404 page at least, but is unable to reach index.html.

My main class to launch the server looks like this, and works fine locally when run through the IDE on localhost:

public static void main(String[] args) {
    Server server = new Server(8080);

    ServletContextHandler servletContextHandler = new ServletContextHandler(NO_SESSIONS);
    servletContextHandler.setContextPath("/");

    DefaultServlet defaultServlet = new DefaultServlet();
    ServletHolder holderPwd = new ServletHolder("default", defaultServlet);

    final URL htmlDirectory = JerseyApplication.class.getResource("/html");

    holderPwd.setInitParameter("resourceBase", htmlDirectory.getFile());

    servletContextHandler.addServlet(holderPwd, "/*");
    server.setHandler(servletContextHandler);

    ServletHolder servletHolder = servletContextHandler.addServlet(ServletContainer.class, "/api/*");
    servletHolder.setInitOrder(0);
    servletHolder.setInitParameter(
            "jersey.config.server.provider.packages",
            "com.x.y.z.parser");

    try {
        LOGGER.info("Starting server");
        server.start();
        server.join();
    }
    catch (Exception ex) {
        LOGGER.error("Server failed to start - Aborting");
        ex.printStackTrace();
    }
    finally {
        LOGGER.info("Destroying server");
        server.destroy();
    }
}

All html stuff is in a the src/main/resources/html directory.

When I run jar tvf jarfile.jar | grep html I can see the html directory and it's contents are in there:

0 Thu Nov 01 11:48:46 UTC 2018 html/
2258 Thu Nov 01 11:48:46 UTC 2018 html/formRequest.js
871 Thu Nov 01 11:48:46 UTC 2018 html/index.html

Thanks!

1 Answers1

1

Use the URL you got from htmlDirectory as the Base Resource for the entire ServletContextHandler.

See prior answer for details: https://stackoverflow.com/a/39019797/775715

final URL htmlDirectory = JerseyApplication.class.getResource("/html");

// TODO: Handle error if htmlDirectory == null

ServletContextHandler servletContextHandler = new ServletContextHandler(NO_SESSIONS);
servletContextHandler.setContextPath("/");
servletContextHandler.setBaseResource(Resource.newResource(htmlDirectory));

DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holderPwd = new ServletHolder("default", defaultServlet);
// holderPwd.setInitParameter("resourceBase", htmlDirectory.getFile()); <-- not needed here
servletContextHandler.addServlet(holderPwd, "/"); // NOTE: MUST be "/" not "/*"!

One last thing, you seem to be using Jersey. (aka jersey.config.server.provider.packages) Make sure you DISABLE the Jersey configurations that make Jersey serve static content itself. Let Jetty be responsible. (as to how this is done, that's another question, which is Jersey version specific and has answers already on stackoverflow)

Joakim Erdfelt
  • 41,193
  • 5
  • 78
  • 124
  • This works! I realised the main issue seemed to be with my deployment strategy and needed to publish ports between the Docker container I was running the .jar file from and the host. – notfreshprince Nov 01 '18 at 14:18