-1

I managed to follow some tutorial and setup a jetty webserver that runs with Maven in Intellij. It runs when I click into maven and do Jetty:run but does not run from my main class. My issue is when try and run the code to launch a webserver I get an error that says Cannot invoke "java.net.URL.toURI()" because "webAppDir" is null. I tried looking at different tutorials but they all use the same structure I set my project up with but they do not encounter this error.

enter image description here

I don't know why it's pointing to a null directory for me but it worked for them, so any help would be greatly appreciated.

Thank you!

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
bpr98
  • 1
  • 1

1 Answers1

0

You appear to be using Jetty 9.4.28.

Jetty 9.4.x is at End of Community Support

You should be upgrading your version of Jetty.

Looking at your code, you are attempting to get a resource by referencing a directory, not a file.

URL webAppDir = Driver.class.getClassLoader().getResource("META-INF/resources");

That's an unreliable technique to reference content in your classloader, as that API prefers that you reference a file, not a directory.

Try referencing a file in that directory, and then stripping it off to use it.

Example from embedded-jetty-cookbook DefaultServletFileServer.java

// Figure out what path to serve content from
ClassLoader cl = DefaultServletFileServer.class.getClassLoader();
// We look for a file, as ClassLoader.getResource() is not
// designed to look for directories (we resolve the directory later)
URL f = cl.getResource("static-root/hello.html");
if (f == null)
{
    throw new RuntimeException("Unable to find resource directory");
}

// Resolve file to directory
URI webRootUri = f.toURI().resolve("./").normalize();
System.err.println("WebRoot is " + webRootUri);

context.setBaseResource(Resource.newResource(webRootUri));
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136