oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Mixing Java and Titanium, Part Two

by James Duncan Davidson

Editor's note: Recently, James Duncan Davidson published a series of weblogs on the subject of Java and Mac OS X for O'Reilly Network. These 'blogs contained lots of useful information that was too good to get buried deep within the network labyrinth, so we've updated them and are now presenting the content in a series of three articles. This is part two of that series.

In this article Davidson discusses MRJAppBuilder and application bundle internals, experiences with Project Builder, and fun with Quartz.

MRJAppBuilder and Application Bundle Internals

The easiest way of creating a double-clickable Java program on Mac OS X is to use Apple's MRJAppBuilder. It creates a nifty bundle which contains all of the class and resource files needed by an application, as well as some information that helps Mac OS X run it. Unfortunately, the documentation on this tool is not as accessible, or as clear, as one might like. In a very uncharacteristic move for Apple, there isn't even a help menu.

Luckily, MRJAppBuilder is not very hard to use. After a couple of runs with the tool, I have come up with the following easy to use recipe:

  1. In the Application tab, fill in the Main Classname text field with the main class of your application. This is the same classname that you'd pass to the Java Virtual Machine on the command line.
  2. Also in the Application tab, fill in the Output File text field with the path and file name you would like your application to have.
  3. Click over to the Merge tab and add all the Jar files of your application.
  4. Click back to the Application tab and make sure that your recently added Jar files are on the classpath.
  5. Build the Application with the aptly named Build Application button.

After following the above process, you will have an application ready to use or distribute. To be more accurate, you will have an application bundle. This bundle will look to users, via the Finder, like a single file, which aids its ease of use and distribution. But under the covers is an interesting hierarchy of files that contain the secrets of how your Java application is run when you double-click on it.

You can dig into the innards of your application bundle in one of two ways. The first is to control-click on the bundle and select Show Package Contents from the pop up menu. This will open up a Finder window that you can browse the contents of the bundle with. The other way is to dive in using the Terminal. Just cd [appname].app (the .app extension is hidden by the Finder, but all application bundles have it). The following is an example of one of the application bundles that I created using MRJAppBuilder:

The Info.plist file is an XML-based description of some basic information that Mac OS uses to run the application. Most of the information in this file only makes sense if you have experience with building native Mac OS applications. The key configuration data it contains (for our purposes) tells the system to execute the JavaApplicationStub executable in the bundle. This stub is a very simple program that it copied from the JavaVM.framework. It looks at the file and then starts up a Java virtual machine based on the configuration information it finds there. Here are the important properties from my application:

Comment on this articleAll three of these topics are interesting, but it's particularly cool to be able to build a double-clickable Java app on Mac OS X. Any thoughts or experiences?
Post your comments

Previously in this series:

Mixing Java and Titanium: Part 1

Having access to all of this information in plain text files means that I can continue to use this created bundle even as my application is updated. For example, if I need to add a library in a Jar file, I can copy it into the Contents/Resources/Java directory of the bundle and add it to the classpath entry in the properties file. Even better, since the application bundle format is so simple, it is possible to set up a few simple build scripts and have bundles created on demand from automated build tools. This bypasses the need to manually execute MRJAppBuilder and recreate application bundles every time a program is updated.

Pages: 1, 2

Next Pagearrow