1

I have the following code:

    Server server = new Server(9090);

    final URL warUrl = Main.class.getClassLoader().getResource("com/domain/webapps/app");
    final String warUrlString = warUrl.toExternalForm();
    WebAppContext wac = new WebAppContext(warUrlString, "/app");

    server.setHandler(wac);

I have the Main class in the com.domain package.

The jsp's and html's are in the com.domain.webapps.app package.

When run inside Netbeans (or java -cp <classpath> com.domain.Main on the exploded jar) the application works perfectly.

If i run the jar (java -jar app.jar), the content of the com.domain.webapps.app gets extracted to /tmp/Jetty_something/webapp/, so the full path is /tmp/Jetty_something/webapp/com/domain/webapps/app/

But when i make a request for http://localhost:9090/app/file.jsp, Jetty tries to get the file from /tmp/Jetty_something/webapp/file.jsp (the wrong place :-( )

What can i do where ?

Jetty version is 6.1.26

skaffman
  • 398,947
  • 96
  • 818
  • 769
paf.goncalves
  • 477
  • 5
  • 18
  • **2019-06-28** Please take a look at this [answer,](https://stackoverflow.com/questions/12672684/starting-up-embedded-jetty-server-for-a-jar-file/56812485#56812485) for a possible solution. – Oliver Jun 28 '19 at 19:50
  • Just tried your suggestion and the error is exactly the same – paf.goncalves Jun 30 '19 at 12:31

1 Answers1

0

Have a look at this article. The URL is detected by

ProtectionDomain protectionDomain = Start.class.getProtectionDomain();
URL location = protectionDomain.getCodeSource().getLocation();

This works for me in a war project and maybe also for your jar use case.

FrVaBe
  • 47,963
  • 16
  • 124
  • 157