Skip to content

Creating a self executing war

glenford edited this page Jul 14, 2011 · 1 revision

Note: This only applies to *nix and OSX platforms, I don't know if it can be done on Windows.

An interesting note about jar (and hence war) files is that they are really a type of zip file, which have the characteristic that they will ignore 'junk' at the beginning of the file. This makes it possible to embed a script at the start of a jar or war and use that script to it execute itself with java.

Take for example the test embed project, you can make this self executing through the following:

echo -e '#!/bin/sh\n\nexec java -jar $0 "${@}"\n\n\n\n' > ./jetty-embed-test
cat xsbt-jetty-embed-test_2.8.1-0.1-jettyEmbed.war >> ./jetty-embed-test
chmod +x ./jetty-embed-test

This provides you a single executeable file that you can run with

$ ./jetty-embed-test
2011-07-12 11:55:01.199::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
2011-07-12 11:55:01.200::INFO:  jetty-6.1.x
2011-07-12 11:55:01.314::INFO:  Extract file:/data/home/glefor/UserSource/xsbt-jetty-embed-test/target/scala-2.8.1.final/xsbt-jetty-embed-test_2.8.1-0.1-jettyEmbed.war to /tmp/Jetty_0_0_0_0_8080_xsbt.jetty.embed.test_2.8.1.0.1.jettyEmbed.war____7mm45i/webapp
2011-07-12 11:55:01.612::INFO:  NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet
12-Jul-2011 11:55:01 com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  net.usersource.jaxrs
12-Jul-2011 11:55:01 com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class net.usersource.jaxrs.Simple
12-Jul-2011 11:55:01 com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
12-Jul-2011 11:55:01 com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.3 06/17/2010 04:53 PM'
2011-07-12 11:55:02.469::INFO:  Started [email protected]:8080

Obviously the script that you embed at the start of the file can do anything you need, so there you can set your Java Home, Jetty options, JVM options and the like.

Clone this wiki locally