The Artima Developer Community
Sponsored Link

Java Buzz Forum
Making Really Executable Jars

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Brian McCallister

Posts: 1282
Nickname: frums
Registered: Sep, 2003

Brian McCallister is JustaProgrammer who thinks too much.
Making Really Executable Jars Posted: Jun 20, 2011 2:12 PM
Reply to this message Reply

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
Latest Java Buzz Posts
Latest Java Buzz Posts by Brian McCallister
Latest Posts From Waste of Time

Advertisement

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 "$@"

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
$ chomd +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…

Read: Making Really Executable Jars

Topic: What's the future of OpenOffice.org? Previous Topic   Next Topic Topic: Obama's CTO eyes cloud, mobile options

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use