I have a multi project build using Gradle in the form:
project1
--build.gradle
--settings.gradle
project2
--src
----main
------java
--------LocalJettyRunner.java
------webapp
--------static
----------index.html
When I run my LocalJettyRunner
and load localhost:8135
I get a 404 when I am expecting my ResourceHandler
to return my Index.html. I have debugged the ResourceHandler.handle(...)
method it appears to be looking in my project1/static
directory which obviously doesn't exist. What am I doing wrong?
LocalJettyRunner
public class LocalJettyRunner {}
public void start() throws Exception {
log.info("Starting api");
ResourceHandler webHandler = new ResourceHandler();
webHandler.setDirectoriesListed(true);
webHandler.setResourceBase("static/");
webHandler.setWelcomeFiles(new String[]{"index.html"});
HandlerCollection handlers = new HandlerCollection();
handlers.addHandler(webHandler);
Server server = new Server(8135);
server.setHandler(handlers);
server.start();
}
public static void main(String[] args) throws Exception {
new LocalJettyRunner().start();
}
}
ResourceBase is an absolute path and/or URL to an absolute path.
The fact that it works without an absolute path is because of how the
new File(String).toAbsolutePath()
works.Try looking up a resource that you know is in the ResourceBase, via the ClassPath, and then using the URL reference to set an absolute path.
Example:
Assuming you have a
src/main/resources/webroot/
, with your static content in it (such asindex.html
), then you can resolve it first, then pass it into the base resource.Incidentally, you have a
src/main/webapp/
which tends to indicate a proper webapp / war file for Maven. For a full blown webapp, it might be easier to skipResourceHandler
and just useWebAppContext
directly (it wires everything up, and does not useResourceHandler
, but ratherDefaultServlet
, which is better all around for serving static files)