liquid liquid
java

How to Create a Mac OS X Installer for a Java Application
(Updated for Mac OS X 10.8 — Mountain Lion)

This information is for an older version of Mac OS X.
For more current information, visit: Mac Java

With some simple steps you can turn your Java Swing program (.jar) into a proper Mac OS X application with a native installer.  The instructions below step you through the process from scratch with a sample program called "Show Time", which simply displays the current time.  Once you have successfully completed the tutorial with the sample Java program, modify the steps to work for your Java program.

Screenshot
Icons
↓ 

1) Launch Unix Terminal and Get Java

Using Finder go into "Applications" and then open the "Utilities" folder.  Open Terminal and you're now at the Unix prompt.  Enter the following command to see if you already have Java installed:
java -version

You will be prompted to download and install Java if needed.

2) Make Project Folder

Enter these two commands:
mkdir showtime
cd showtime

The first command creates a folder called "showtime", and the second command moves you into the new folder.

3) Write Some Java Code

Mac OS X comes with a simple but effective text editor called Nano.  Use the following command to create and edit a new Java file:
nano ShowTime.java
Enter the following code:

ShowTime.java

import java.util.Calendar; import javax.swing.*; public class ShowTime { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("It's Showtime!"); frame.getContentPane().add(new JLabel( Calendar.getInstance().getTime().toString())); frame.pack(); frame.setVisible(true); } }

Use <control-x> to exit Nano.

4) Compile Java Program

Back at the Unix prompt, compile the Java program into a class file:
javac ShowTime.java
ls -l

It is possible to run the class file directly, but that results in a poor user experience.  Instead we will create a more convenient executable JAR file.

5) Make Executable JAR

Before we make an executable JAR file, we need a manifest file to indicate which class contains the "main" function.  Use Nano again:
nano MainClass.txt
The manifest file only needs the single line:

MainClass.txt

Main-Class: ShowTime

Exit Nano and use the following jar command to create the "ShowTime.jar" file:
jar cmf MainClass.txt ShowTime.jar *.class
ls -l
Now test your executable JAR with the following command:
java -jar ShowTime.jar

The "It's Showtime!" window with the current time should display in the upper left corner of the screen.  Click the red button (marble) to exit the program.

Note: While the manual commands above for steps #4 and #5 work fine, you could automate them using Ant with this build.xml file.

6) Create Application Icon

The default icon for an executable JAR is a coffee cup.  To add a custom icon, we need to create an .icns file.

Download and save (<control-click>) this sample PNG image to your "Desktop":  ShowTime.png

Then use the following commands to move the file into the "showtime" folder, resize the image to appropriate dimensions for an icon, and convert it into the .icns format:

mv ../Desktop/ShowTime.png .
mkdir ShowTime.iconset
sips -z 32 32 ShowTime.png --out ShowTime.iconset/icon_32x32.png
iconutil -c icns ShowTime.iconset
ls -l

Note: Warnings from the iconutil command just indicate that the icon will not display optimally at some sizes.

7) Bundle the JAR

Bring up Spotlight with the <command-space> keystroke and type "jar bundler" to find and the Jar Bundler application (it's in the "/usr/share/java/Tools" folder).  Launch the application.

Steps:
  1. For the "Main Class:", use the "Choose..." button and go to and choose "ShowTime.jar".
  2. Check the "Use Macintosh Menu Bar" option.
  3. Using Finder, go to the "showtime" folder and drag the "ShowTime.icns" file into the "Custom Icon" box.
  4. Click the "Properties" tab and enter "1.0" into both the "Version:" field and the "Get-Info String:" field.
  5. Click the "Create Application..." button.
  6. Navigate into the "showtime" folder.
  7. In the "File:" field, enter "Show Time".
  8. Click the "Create" button.
  9. Quit Jar Bundler.

We now have a proper Mac application.  Next we'll create an installer for the application.

8) Create Mac Installer

Return to Terminal and use the productbuild tool to create the installer:
productbuild --component "Show Time.app" /Applications show-time-installer-1.0.pkg
ls -l
rm -rf "Show Time.app"

The rm command deletes the original application bundle so that the installer will install into /Applications instead of just overwriting the original version.

Note: When you are ready to distribute to the public, you'll want to sign your application with a Developer ID certificate.  Use the productsign tool to apply a certificate.

9) Put Installer on a Web Page

Now build a test download page to verify the installer will work correctly:
nano download.html
The HTML for the test page is:

download.html

<html> <body> Download: <a href=show-time-installer-1.0.pkg>show-time-installer-1.0.pkg</a> </body> </html>

Exit the editor and saving the web page file.  Now copy the file and the installer file to the document root folder of the Apache web server and start the web server:
sudo cp *.html *.pkg /Library/WebServer/Documents/
sudo apachectl start

Note: You will be prompted to enter your password when using the sudo command.  If your password is rejected, then you do not have root privileges.  First login as an administrator using:  su - <username>

The Apache web server is now running, and your download page is at:

Click the "show-time-installer-1.0.pkg" link.  Now look in your "Downloads" folder and double-click the installer file.  You will be presented with a "unidentified developer" security error.  Click "Ok" and then control-click the installer file and choose "Open".

After completing the installation, go into the "Applications" folder and run the "Show Time" application.  Be sure to check out the "About Show Time" option on the "Show Time" menu.

Troubleshooting

If your application does not install and run properly, the first place to look is step 5, which creates the JAR file.  Try double-clicking the JAR file to launch your application.  If it fails to launch, you need to fix that before continuing.

The next place to look is step 7, which creates the application file.  Try double-clicking the .app file.  If it fails to launch, you need to fix that before continuing.

Do the same for step 8, which creates the installer file (.pkg).

Wrap-Up

For an example of how you might distribute your installer, take a look at:
If you want to add a "Visit Website" button to your application, check out:

That's it.

liquid liquid