Setting Jetty resourcebase to static file embedded in the same jar file

雨燕双飞 提交于 2020-01-03 15:05:34

问题


I am trying to access static resource (eg. first.html) packed inside the same .jar file (testJetty.jar), which also has a class which starts the jetty (v.8) server (MainTest.java). I am unable to set the resource base correctly.

The structure of my jar file (testJetty.jar): testJetty.jar

  • first.html

  • MainTest.java

== Works fine on local machine, but when I wrap it in jar file and then run it, it doesn't work, giving "404: File not found" error.

I tried to set the resourcebase with the following values, all of which failed:

a) Tried setting it to .

resource_handler.setResourceBase("."); // Results in directory containing the jar file, D:\Work\eclipseworkspace\testJettyResult

b) Tried getting it from 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

c) Tried getting the webdir

String webDir = this.getClass().getProtectionDomain()
        .getCodeSource().getLocation().toExternalForm();
resource_handler.setResourceBase(webdir); // Results in D:/Work/eclipseworkspace/testJettyResult/testJetty1.jar

None of these 3 approaches worked.

Any help or alternative would be appreciated

Thanks abbas


回答1:


The solutions provided in this thread work but I think some clarity to the solution could be useful.

If you are building a fat jar and use the ProjectionDomain way you may hit some issues because you are loading the whole jar!

class.getProtectionDomain() .getCodeSource().getLocation().toExternalForm();

So the better solution is the other provided solution

contextHandler.setResourceBase(YourClass.class.getClassLoader().getResource("WEB-INF").toExternalForm());

The problem here is if you are building a fat jar you are not really dumping your webapp resources into WEB-INF but are probably going into the root of the jar, so a simple workaround is to create a folder XXX and use the second approach as follows:

contextHandler.setResourceBase(YourClass.class.getClassLoader().getResource("XXX").toExternalForm());

Or change your build tool to export the webapp files into that given directory. Maybe Maven does this on a Jar for you but gradle does not.




回答2:


Not unusually, I found a solution to my problem. The 3rd approach mentioned by Stephen in Embedded Jetty : how to use a .war that is included in the .jar from which Jetty starts? worked!

So, I changed from Resource_handler to WebAppContext, where WebAppContext is pointing to the same jar (testJetty.jar) and it worked!

    String webDir = MainTest.class.getProtectionDomain()
            .getCodeSource().getLocation().toExternalForm(); ; // Results in D:/Work/eclipseworkspace/testJettyResult/testJetty.jar
    WebAppContext webappContext = new WebAppContext(webDir, "/");



回答3:


It looks like ClassLoader.getResource does not understand an empty string or . or / as an argument. In my jar file I had to move all stuf to WEB-INF(any other wrapping dir will do). So the code looks like

contextHandler.setResourceBase(EmbeddedJetty.class.getClassLoader().getResource("WEB-INF").toExternalForm()); so the context looks like this then:

ContextHandler:744 - Started o.e.j.w.WebAppContext@48b3806{/,jar:file:/Users/xxx/projects/dropbox/ui/target/ui-1.0-SNAPSHOT.jar!/WEB-INF,AVAILABLE}



来源:https://stackoverflow.com/questions/25660467/setting-jetty-resourcebase-to-static-file-embedded-in-the-same-jar-file

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