Embedded jetty application not working from jar

2019-08-06 19:54发布

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

1条回答
看我几分像从前
2楼-- · 2019-08-06 20:28

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.

查看更多
登录 后发表回答