con*_*bas 5 jar jetty embedded-jetty
我正在尝试访问包装在同一.jar文件(testJetty.jar)中的静态资源(例如first.html),该文件也具有启动码头(v.8)服务器(MainTest.java)的类。我无法正确设置资源库。
我的jar文件(testJetty.jar)的结构:testJetty.jar
first.html
MainTest.java
==在本地计算机上工作正常,但是当我将其包装在jar文件中然后运行时,它不起作用,并出现“ 404:找不到文件”错误。
我尝试使用以下值设置资源库,所有这些都失败了:
a)尝试将其设置为。
resource_handler.setResourceBase("."); // Results in directory containing the jar file, D:\Work\eclipseworkspace\testJettyResult
Run Code Online (Sandbox Code Playgroud)
b)尝试从getResource获取它
ClassLoader loader = this.getClass().getClassLoader();
File indexLoc = new File(loader.getResource("first.html").getFile());
String htmlLoc = indexLoc.getAbsolutePath();
resource_handler.setResourceBase(htmloc); // Results in D:\Work\eclipseworkspace\testJettyResult\file:\D:\Work\eclipseworkspace\testJettyResult\testJetty1.jar!\first.html
Run Code Online (Sandbox Code Playgroud)
c)尝试获取webdir
String webDir = this.getClass().getProtectionDomain()
.getCodeSource().getLocation().toExternalForm();
resource_handler.setResourceBase(webdir); // Results in D:/Work/eclipseworkspace/testJettyResult/testJetty1.jar
Run Code Online (Sandbox Code Playgroud)
这3种方法均无效。
任何帮助或替代将不胜感激
谢谢阿巴斯
不出所料,我找到了解决问题的方法。Stephen 在 Embedded Jetty 中提到的第三种方法:如何使用 Jetty 启动的 .jar 中包含的 .war?成功了!
因此,我从 Resource_handler 更改为 WebAppContext,其中 WebAppContext 指向同一个 jar (testJetty.jar) 并且它有效!
String webDir = MainTest.class.getProtectionDomain()
.getCodeSource().getLocation().toExternalForm(); ; // Results in D:/Work/eclipseworkspace/testJettyResult/testJetty.jar
WebAppContext webappContext = new WebAppContext(webDir, "/");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3248 次 |
最近记录: |