嵌入式Jetty应用程序无法在Jar中运行 [英] Embedded jetty application not working from jar

查看:94
本文介绍了嵌入式Jetty应用程序无法在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆