1

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();
        }
}
EarlyDev
  • 31
  • 5

2 Answers2

2

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);
EarlyDev
  • 31
  • 5
0

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

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • That does not work either. When I run it from my IDE localhost:2222 brings up my index.html page as intended. But when I run it from the jar it does not. I think that jersey is missing from the jar but I don't know enough about java to check if that is true. – EarlyDev Dec 12 '17 at 09:29
  • Is your index.html in your jar contained in a `src/Web` folder? – Joakim Erdfelt Dec 12 '17 at 21:05
  • yes, i have found the issue now will post the answer now – EarlyDev Dec 13 '17 at 13:17