views:

637

answers:

4

Hey people, it's my first question here.

I successfully embedded Jetty on a test application. It can serve files without issues. Now I want to know if it's possible for Jetty to serve files that are inside its own Jar file.

Does anyone know if that's possible?

Thanks!

A: 

Maybe more of a hack, but aren't JAR files actually ZIPs? (not sure) Could you unzip them into a temporary folder and serve them from there?

lod3n
I could, but I want a self-contained web application inside a Jar for simplicity purposes. :)
LaSombra
A: 

Found the answer and it's not Jetty, it's Winstone. http://winstone.sf.net

LaSombra
+1  A: 

An example is listed on the Jetty embedding page at http://docs.codehaus.org/display/JETTY/Embedding+Jetty

The trick is to create a File URL to your classpath location.

String webDir = this.class.getClassLoader().getResource("com/company/project/mywebdir").toExternalForm();

ServletContextHandler context = new ServletContextHandler(); context.setContextPath("/"); context.setResourceBase(webDir);

Uriah Carpenter
A: 

It's pretty simple, if you throw Spring into the equation. And here it goes:

 ...

 WebAppContext webAppContext = new WebAppContext();
 webAppContext.setServer(server);
 webAppContext.setContextPath("/");
 webAppContext.setResourceBase(new ClassPathResource("webapp").getURI().toString());

 server.addHandler(webAppContext); 

 ....

That will make jetty find the necessary web resources inside the jar file.

James Selvakumar
Thanks. I did it with Maven + Assembly plugin :)
LaSombra