Embedded jetty application not working from jar

核能气质少年 提交于 2019-12-11 06:54:21

问题


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:


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.



来源:https://stackoverflow.com/questions/5329076/embedded-jetty-application-not-working-from-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!