liquid liquid
mac java
mac-java

How to Create a Mac OS X Installer for a Java Application
(Updated for Mac OS X 10.5 — Leopard)

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

Screenshot
Icons
↓ 

"It's Showtime!" 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.

1) Install Xcode

Apple's Xcode suite includes development tools you'll need to bundle and package a Java program.  First, download Xcode for Mac Development (version 3.1.1 or later) and open the downloaded .dmg file.  Now run the "XcodeTools.mpkg" file and complete the Xcode installation with all the default options.

Before continuing to the next step, it's a good idea to perform a "Software Update..." to make sure your OS files are current.

2) Launch Unix Terminal

Using "Finder" go into "Applications" and then open the "Utilities" folder.  Scroll down until you see "Terminal".  Open "Terminal" and you're now at the Unix prompt.

3) Make Project Folder

At the Unix prompt, enter these two commands:
mkdir ItsShowtime
cd ItsShowtime

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

4) Write Some Java Code

Mac OS X comes with a simple but effective text editor called Pico.  Use the following command to create and edit a new Java file:
pico 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 <ctrl-x> to exit Pico.

5) Compile Java Program

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

We could run the class file directly, but a class file is cumbersome.  Instead we will create an executable JAR file.

6) Make Executable JAR

Before we make an executable JAR file, we need a manifest file to indicate which class contains the "main" function.  We'll use Pico again:
pico MainClass.txt
Our manifest file will only have one line:

MainClass.txt

Main-Class: ShowTime
Exit Pico and use the following "jar" command to create the "ShowTime.jar" file:
jar cmf MainClass.txt ShowTime.jar *.class
ls -la
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 dot to exit the program.

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

7) Create Application Icon

The default icon for an executable JAR is a coffee cup.  To add a custom icon, we need to use the "Icon Composer".

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

Then move the file into the "ItsShowtime" folder with the following command:
mv ../Desktop/ShowTime.png .

Now we can create the icon file.

Steps:
  1. Use "Finder" to navigate into the "Developer:Applications:Utilities" folder and double-click "Icon Composer".
  2. Go back to "Finder" and navigate to your "ItsShowtime" folder (which is in your home folder).
  3. Drag the "ShowTime.png image file into the "128" box on the "Icon Composer" screen.  When prompted about sizes, choose "Copy to all smaller sizes" and then click the "Import" button.
  4. Go into the "File" menu and select the "Save" option.  Then deselect the "Hide extension" option and save as "ShowTime.icns".
  5. Quit "IconComposer".

Next we'll create a Mac application (with your new icon).

8) Bundle the JAR

Using "Finder", navigate into the "Developer:Applications:Utilities" folder and double-click "Jar Bundler".

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. Use the "Choose Icon..." button to choose the "ShowTime.icns" file (you'll need to navigate to the very top-level folder and then into the "Users" folder and your home folder to eventually find the "ItsShowtime" folder).
  4. Click the "Properties" tab and enter "1.0" into the "Version:" field.
  5. Also enter "1.0" into the "Get-Info String:" filed.
  6. Click the "Create Application..." button.
  7. Navigate into the "ItsShowtime" folder.
  8. In the "File:" field, enter "Show Time".
  9. Click the "Create" button.
  10. Quit "Jar Bundler".

You now have a proper Mac application.  Next we'll create an installer for your application.

9) Create Mac Installer

Using "Finder", navigate into the "Developer:Applications:Utilities" folder and double-click "PackageMaker".

Steps:
  1. In the "Organization:" field on the "Install Properties" window, enter "com.centerkey".  Then click "OK".
  2. In the "Title:" field enter "Show Time".
  3. Go to the "Project" menu and select "Add Contents...".  Navigate to "ItsShowtime" folder and select "Show Time".
  4. Click on the "Contents" tab.  Check the "Include root in package" option and click the "Apply Recommendations" button.
  5. Now click the "Build" (hammer) button.  In the "Save As:" field, enter "ShowTimeInstaller.pkg".  Click the "Save" button and then the "Return" button.
  6. Go to the "File" menu and select "Save".  In the "Save As:" field, enter "ShowTime.pmdoc" and then click "Save".
  7. Quit "PackageMaker".

Your installer is done, but it's not yet download friendly.

10) Put Installer on a Web Page

Before putting the installer on the web, we need to zip it up into a single file.  Use "Finder" to navigate to the "ItsShowtime" folder.  Create a zip of "ShowTimeInstaller.pkg" using the "Compress" option on the <ctrl-click> menu.

Back at the Unix prompt in the "Terminal", create a test web page:
pico download.html
The HTML for the test page is:

download.html

<html> <body> Download: <a href=ShowTimeInstaller.pkg.zip> ShowTimeInstaller.pkg.zip</a> </body> </html>

After exiting Pico and saving the web page (.html) file, copy it and the .zip file to your personal web server folder with the command:
cp *.html *.zip ../Sites

Now we need to turn on the Mac's Apache web server.

Steps:
  1. Go to the "Apple" menu (Apple) and choose "System Preferences..."
  2. In the "Internet & Networking" section, click "Sharing"
  3. Check the option for "Web Sharing" service
  4. Quit "System Preferences"

Your Apache web server is now running.

Launch Safari and go to  http://localhost/~you/download.html  where "you" is your user name.  Click the "ShowTimeInstaller.pkg.zip" link and the install should automatically start within a few seconds.  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.

Troubleshooting

If your application does not install and run properly, the first place to look is step 6, 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 8, 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 9, 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