Making Really Executable Jars

One of the more annoying things about writing command line applications in Java is that the Java model of an executable is the so called executable jar, which is executed via an incantation like

$ java -jar ./waffles-1.2.3.jar --some-flag=blue hello 

There has long been a hack known in some circles, but not widely known, to make jars really executable, in the chmod +x sense. The hack takes advantage of the fact that jar files are zip files, and zip files allow arbitrary cruft to be prepended to the zip file itself (this is how self-extracting zip files work).

To do this for jar files, on unix-like operating systems, create a little shell script which looks like:

#!/bin/sh

exec java -jar $0 "$@"

You can make it fancier, doing things like looking for JAVA_HOME and so on, but the above is enough to get started. Make sure to add a few newlines at the end, they are very important. If you leave them out it will not work.

Now that you have your little shell script, cat the executable jar you want onto the end of it, set the script +x, and go to town. If you script is named waffles, then you would do that like:

$ cat ./waffles-1.2.3.jar >> ./waffles
$ chmod +x ./waffles
$ ./waffles --some-flag=blue hello

and there you go! I have a little maven plugin that will do this for you automagically, but haven’t had a chance to get it into central yet. I guess I should probably stop writing and go do so…

Updates

  • David Phillips suggested putting the $@ in parens as it can contain spaces. I have updated the post to do so.

  • Sven Schober pointed out a bug in the original form of the shell script I posted. I forgot the extremely important $0. That is what I get for writing from memory and not unit testing my blog posts! The post has been fixed.

  • Jeffrey McManus found a typo, I had chomd instead of chmod. Fixed, thank you! I really need to find a way to unit test blog posts!