Creating an OS X Application from a Java JAR

On OS X you can run a JARed Java App by simply double clicking the .jar file. This works but it has a few downsides. For a start you can’t keep the app in your dock so you can launch it easily and you also can’t easily launch it with things like HimmelBar. Secondly it will always have the Java JAR icon and if you’re running a few Java JARs this can get very confusing. What you really want is the same Java App but wrapped inside an OS X App. Examples of this would be the OS X version of jEdit. If you have the Developer Tools (XCode) installed on OS X this is trivial to do. If you don’t have the developer tools installed you’ll find and installer for them on your OS X DVD.

[tags]OS X, Java, Application[/tags]

I’m going to explain the process with a worked example. I’m going to wrap the freeware Java version of HJSplit which you can get from www.freebyte.com/hjsplit/ into an OS X App. We’ll need two apps to get this done, Jar Bundler (Developer -> Applications -> Java Tools -> Jar Bundler) and the Icon Composer (Developer -> Applications -> Utilities -> Icon Composer).

The first step is to create an OS X icon file for our new App. When I need to pick an Icon I generally do a google image search on a related term and then pick something. In this case I’m going to use the FreeByte logo since HJSplit is released by FreeByte. When ever you have an image file you like you need to edit it with your favourite image manipulation program (I use the GIMP) so that it is 128 x 128 pixels and saved as a PNG. To make the logo look good it should also be on a transparent background. Note that if you save the image as a GIF instead of a PNG the transparency you set will be lost when you try to use the image in the Icon Composer. Once you have your image saved in the correct format launch the Icon Composer. When it opens it will create a new icon file which looks like a collection of empty boxes. These are for different sized versions of your icon but we’re not going to bother with that. We’ll just create a simple icon which contains just one image, the biggest one. To do this drag and drop your image file from the Finder into the biggest of the available boxes in Icon Composer. Then save the file. This will create an Apple Icon Image file from the image (.icns file).

Now that you have your icon created we can go on and create the Application (assuming you actually have the JAR you want to turn into an App of course!). To do this open up the JAR Bundler. Then choose your JAR file and set your icon. Now you need to decide what version of Java your App should be targeted at. At the moment I would advise targeting version 1.4+ of the JVM. You now have one more choice to make, do you want your new App to have it’s menu bar items in the Apple Menu Bar at the top of the screen like regular OS X Apps or at the top of the Apps window like regular Java Apps. To make your choice just check or un-check the Use Macintosh Menu Bar check box. You should note that if the Java app you are wrapping does not use the standard Java GUI classes then the menus may appear at the top of the App’s Window regardless of whether or not you checked the box. Now just click Create Application ... and choose where you want your new App saved. That’s it, you’re done!

Click to Enlarge

Below is a screen shot of the finished product running. Note it has its own icon on the Doc and that it’s menu items are in the Apple Menu Bar.

If you want to distribute your new App you may want to create a Disk Image for it (.dmg file). You can do this with the Disk Utility. Just stick the App and any other files you want bundled (like a licence or a readme) into a folder, then open Disk Utility and select File -> New -> From Folder, point it at your folder, pick a name and you’re done!

Click to Enlarge