Embed jetty in a Java application and export it as a jar
I'm making a Java application embedded in jetty web server, which in turn provides content developed using Google Web toolkit It runs normally in eclipse, but when I export it as a jar file, I get a jetty error message "file not found"
The startup method of jetty server is as follows:
WebAppContext handler = new WebAppContext();
handler.setResourceBase("./war");
handler.setDescriptor("./war/WEB-INF/web.xml");
handler.setContextPath("/");
handler.setParentLoaderPriority(true);
server.setHandler(handler);
try {
server.start();
server.join();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I suspect the problem is handler Setresourcebase() and handler Relative path used in setdescriptor() I have searched and tested many solutions with Google, but so far it has not helped In particular, I've tried to use things like getclass() getResource(“./ war”). To externalform(), but this will only throw null exceptions
I also tried:
ProtectionDomain protectionDomain = Start.class.getProtectionDomain(); URL location = protectionDomain.getCodeSource().getLocation();
But this will only cause jetty to serve the directory list of Java classes
Is there any way to do this?
Solution
>Copy all the files of the compiled GWT application into a java package For example:
my.webapp.resources html/MyPage.html gwtmodule/gwtmodule.nocache.js ...
(HTML and gwtmodule will also become Java packages). > Configure the embedded jetty instance to provide these files
final String webDir = this.getClass().
getClassLoader().getResource("my/webapp/resources").toExternalForm();
final Context root = new Context(server,"/",Context.SESSIONS);
root.setContextPath("/");
root.setResourceBase(webDir);
root.addServlet(MyGwtService.class,"/servlets/v01/gwtservice");
This approach works for me when applications are running and deployed from eclipse
