This post originated from an RSS feed registered with Java Buzz
by Brian McCallister.
Original Post: Making Really Executable Jars
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
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
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/shexec java -jar "$@"
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:
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…