问题
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