'Runnable Jar file not finding bundled resources

I have a simple web server (implemented in Vertx) which serves static content composed of HTML, CSS, and Javascript files. I have developed this server in the latest Eclipse IDE.

The server works well in the development environment, but when I generate a runnable Jar file, the jar file fails to "find" the web pages.

The Vertx Web code that I am using is pretty straightforward. I am using the static content method as provided on the Vertx Web documentation:

  HttpServer server = vertx.createHttpServer();
  
  Router router = Router.router(vertx);
  
  router.route(HttpMethod.GET,"/*").handler(StaticHandler.create(".").setIndexPage("index.html"));
  
  server.requestHandler(router);
  
  server.listen(8099,result->{
     if(result.succeeded())
        log.info("Main Server has started! Listening on Port "+server.actualPort());
     else if(result.failed())
        log.error("Main Server start FAILED. Reason: "+result.cause());
  });

The directory structure of my project is below:

  testmain
  |
  --src
  | |
  | --<package net.test.app>
  |   |
  |   --TestServer.java
  |
  --resources
    |
    --index.html
    |
    -- sheet.css
    |
    --script.js

I generate the runnable Jar using the usual method: Export->Runnable Jar File. In the export dialog, I specify the TestServer application and destination Jar, and I select "Extract required libraries into generated jar".

Examining the contents of the runnable Jar file using 7-Zip, I see that all necessary class files, libraries, and static resources are in the Jar file. Unfortunately, when I run the application, the files cannot be found. The only way to make the application find them is to copy them into the same folder as the jar file/ Even then, the Javascript file isn't being found.

I am sure that there is a way to configure the runnable Jar so that the application will look within the jar file for its resources, because other applications (servlet- based or Spring Boot applications) are packaged so that their runnables are self- contained. Unfortunately, neither the Eclipse documentation nor any of the pages describing runnable Jars address this issue.

Those that talk about using resources within a runnable suggest changing the code that accesses the files so that it finds them on the classpath. This is good, but because I am using Vertx web to access the static files, rewriting the code for reading them is not an option.

Is there a way to configure my runnable Jar so that it looks within the jar for its resources? Or failing that, is there a way to make Vertx access them from within the Jar?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source