1
votes

I'm very new to java so I'm not sure why this is happening.

I have developed a small java application using Jersey + Jetty. This works perfectly when running it in intelliJ but doesn't work from the jar. When I go to localhost:2222 jetty should show me a index.html page but instead jetty says No context on this server matched or handled this request.

Jar was created using these instructions How to build jars from IntelliJ properly?

    public class App{
    private static final int SERVER_PORT = 2222;

    public static void main(String[] args) throws Exception {
        URI baseUri = UriBuilder.fromUri("http://localhost").port(SERVER_PORT)
        .build();
        ResourceConfig config = new ResourceConfig(Resource.class);
        Server server = JettyHttpContainerFactory.createServer(baseUri, config,
        false);

        ContextHandler contextHandler = new ContextHandler("/rest");
        contextHandler.setHandler(server.getHandler());

        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setWelcomeFiles(new String[] { "index.html" });
        resourceHandler.setResourceBase("src/Web");

        HandlerCollection handlerCollection = new HandlerCollection();
        handlerCollection.setHandlers(new Handler[] { resourceHandler,
        contextHandler, new DefaultHandler() });
        server.setHandler(handlerCollection);
        server.start();
        server.join();
        }
}
2

2 Answers

2
votes

A couple of things were needed to get this working. Firstly src/web directory wasn't being included in the .jar.

Once that was solved the problem persisted because the code wasn't able to find the location of the index.html page in the .jar.

Changing my code to the following fixed it.

String webDir = this.getClass().getClassLoader().getResource("src/Web").toExternalForm(); 

ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setWelcomeFiles(new String[]{"index.html"});
resourceHandler.setResourceBase(webDir);
0
votes

Your specified context path is /rest in that embedded-jetty code.

The message you see even tells you that.

If you want it to be on root, then your code should be new ContextHandler("/"); instead