What is correct URL to specify ResourceBase of JAR "resources/webapp" folder for embedded Jetty?

will picture will · Aug 6, 2014 · Viewed 8.4k times · Source

We want a simple embedded Jetty servelet with the web resources inside a JAR-file's resources folder. We have some properties files in the JAR and load them using a resources path. We want to specify the Jetty Resource Base to be:

  • resources/webapp
  • set: resource_handler.setResourceBase( "webapp" )
    • Via the correct URL to point to that resource in the JAR file.

Folder in the JAR file. This is a bare bones JAR file (not WAR, no frameworks, without Spring, as vanilla as we may). Initial tests continue to throw exceptions for something like the following strings:

webPath = "jar:file:!/webapp";     //.... runs the Jetty server 
 ... 
resource_handler.setResourceBase( webPath );

Although the server seems to run, the result fails to find my index.html. (Update:) This example is just taking from the Jetty "Embedded File Server" example. In this case, the requirement is for the Jetty Resource Base to map to the JAR file (full-URL):

  • "jar:file:!/webapp/index.html",

as follows:

  • resource_handler.setResourceBase("jar:file:!/webapp");

Instead of the example given:

  • resource_handler.setResourceBase(".");

And we want this to map the browser URL as:

  • localhost:8080/index.html
    • ... giving ...
  • jar:file:!/webapp/index.html

For contrast the JAR path that work for config files below. The question: what should the URL be so Jetty resource base can serve-up my Index.html file?

  • resources/
    • config/
      • display.properties

file is: "/config/display.properties" and this works in the same project code using a load resources operation. The layout is like this:

 app.jar
   +----- com /
   |        +--- ( classes ... )
   |
   +----- config /
   |        |
   |        +--- display.properties
   |
   +----- webapp /
            |
            +--- index.html

To give the general idea.

similar questions:

Answer

will picture will · Aug 7, 2014

I have a working solution - Work-around that I'm posting in the hope that this approach will inspire correct method. I still believe there ought to be a way to specify a folder inside the JAR, relative to the JAR.

Anyway this method works. I used it to server static web content from within the JAR. Essentially I have Java resolve the absolute path to the running JAR resource and pass that path name to Jetty. When I do that Jetty displays my "helloWorld.html", welcome file.

    String  baseStr  = "/webapp";  //... contains: helloWorld.html, login.html, etc. and folder: other/xxx.html
    URL     baseUrl  = SplitFileServerRunner.class.getResource( baseStr ); 
    String  basePath = baseUrl.toExternalForm();

      .... 
    resource_handler.setDirectoriesListed(true);      //... just for testing
    resource_handler.setWelcomeFiles(new String[]{ "helloWorld.html" });
    resource_handler.setResourceBase( basePath );
    LOG.info("serving: " + resource_handler.getBaseResource());

In the welcome file, I have put specific text to identify the file's origin (in the resources folder). In the browser:

  • localhost:8080

Serves the helloWorld.html file.

  • localhost:8080/other

Shows a directory listing of the jar:/webapp/other/ directory inside the JAR file. This relies on not changing the JAR while the server is running.

On Linux if someone cp-s a new jarfile on-top of the running JAR, Jetty gives:

 HTTP ERROR: 500

 Problem accessing /. Reason:

        java.lang.NullPointerException

And you can't access pages any more. That was unexpected (evidently the JAR is kept open). The good news is that if you mv-s the jarfile:

  • mv fileserver.jar fileserverXX.jar

Jetty happily continues serving from the (renamed) fileserverXX.jar content. I can be happy with that. However I'd still like to know the equivalent relative path to match the absolute file name.