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:
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):
as follows:
Instead of the example given:
And we want this to map the browser URL as:
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?
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:
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:
Serves the helloWorld.html file.
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:
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.