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

We need a simple embedded Jetty server with web resources in the resource folder of the JAR files. We have some property files in the JAR and load them using the resource path. We want to specify the Jetty resource base:

  • resources / webapp
  • set: resource_handler. setResourceBase ("webapp")
    • Use the correct URL to specify this resource in the JAR file .

The folder in the JAR file. This is a simple JAR file (no WAR, no frameworks, no Spring, like vanilla, as we can). The original tests continue to throw exceptions for the following lines:

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

Even though the server is running, the result does not find my index.html. ( Refresh ). This example just takes with the Jetty " Embedded File Server ". In this case, it is required that the Jetty Resource Base is displayed in a JAR file (full URL):

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

in the following way:

  • resource_handler.setResourceBase (" jar: file: / web application ");

Instead of the above example:

  • resource_handler.setResourceBase ("");

And we want this to display the browser URL as:

  • local: 8080 / index.html
    • ... providing ...
  • jar: file: /web application/index.html

For contrast, the JAR path that works for the configuration files below. Question: What does a URL need for the Jetty resource base to serve my Index.html file?

  • Resources /
    • config /
      • display.properties

file: "/config/display.properties", and this works in the same project code using the resource load operation. The layout is as follows:

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

To give a general idea.

similar questions:

  • Starting the embedded berth server for the JAR file
+10
java url jar maven jetty-9
source share
2 answers

I have a working solution - Work-around, which I publish in the hope that this approach will inspire the right method. I still believe that there should be a way to specify the folder inside the JAR in relation to the JAR.

In any case, this method works. I used it for static web server content from a JAR. Essentially, I have Java allowing an absolute path to a running JAR resource and passing that Jetty path name. When I do this, Jetty displays my "helloWorld.html", a 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 placed specific text to determine the source of the file (in the resources folder). In browser:

  • local: 8080

Serves for the helloWorld.html file.

  • local: 8080 / other

Displays a list of directories in the jar: / webapp / other / directory inside the JAR file. This depends on not changing the JAR while the server is running.

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

  HTTP ERROR: 500 Problem accessing /. Reason: java.lang.NullPointerException 

And you can no longer access the pages. This was unexpected (obviously, the JAR remains open). The good news is that if you are mv -s jarfile:

  • mv fileserver.jar fileserverXX.jar

Jetty happily continues to work with the (renamed) fileserverXX.jar file. I can be satisfied with that. However, I would still like to know the equivalent relative path to match the absolute file name.

+7
source share

After the initial answer, some methods have changed, so the following steps work the same, but with the Jetty version: 9.4.12.v20180830:

 ServletContextHandler webappContext = new ServletContextHandler(ServletContextHandler.SESSIONS); URL webRootLocation = JettyStart.class.getResource("/webapp/index.html"); URI webRootUri = URI.create(webRootLocation.toURI().toASCIIString().replaceFirst("/index.html$", "/")); webappContext.setContextPath("/"); webappContext.setBaseResource(Resource.newResource(webRootUri)); webappContext.setWelcomeFiles(new String[] { "index.html" }); 
0
source share

All Articles