嵌入式Jetty应用程序无法在Jar中运行 [英] Embedded jetty application not working from jar
问题描述
我有以下代码:
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);
我在com.domain
包中有Main
类.
jsp和html位于com.domain.webapps.app
包中.
The jsp's and html's are in the com.domain.webapps.app
package.
在Netbeans(或爆炸的jar中的java -cp <classpath> com.domain.Main
)内部运行时,该应用程序运行正常.
When run inside Netbeans (or java -cp <classpath> com.domain.Main
on the exploded jar) the application works perfectly.
如果我运行jar(java -jar app.jar
),则com.domain.webapps.app
的内容将提取到/tmp/Jetty_something/webapp/
,因此完整路径为/tmp/Jetty_something/webapp/com/domain/webapps/app/
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/
但是当我请求http://localhost:9090/app/file.jsp
时,Jetty尝试从/tmp/Jetty_something/webapp/file.jsp
获取文件(错误的位置:-()
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 :-( )
我在哪里可以做什么?
码头版本为6.1.26
Jetty version is 6.1.26
推荐答案
Have a look at this article. The URL is detected by
ProtectionDomain protectionDomain = Start.class.getProtectionDomain();
URL location = protectionDomain.getCodeSource().getLocation();
这在war
项目中对我有用,在您的jar
用例中也可能适用.
This works for me in a war
project and maybe also for your jar
use case.
这篇关于嵌入式Jetty应用程序无法在Jar中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!