|
How to Create a Mac OS X Installer for a Java Application
(Updated for Mac OS X 10.6 — Snow Leopard)
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
↓
"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.
1) 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.
2) Make Project Folder
At the Unix prompt, 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 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 f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("It's Showtime!");
f.getContentPane().add(new JLabel(
Calendar.getInstance().getTime().toString()));
f.pack();
f.setVisible(true);
}
}
Use <control-x> to exit Pico.
4) Compile Java Program
Back at the Unix prompt, compile the Java program into a class
file:
javac ShowTime.java
ls -l
We could run the class file directly, but a class file is cumbersome.
Instead we will create an 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. 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 -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 dot to exit the
program.
While the manual commands above for steps #4 and #5 work fine, you could automate
them using
Ant
with this
build.xml
file.
6) Install Xcode for Mac Development
Apple's Xcode suite includes the developer tools you'll need to create an icon and
installer. Download
Xcode for Mac Development
and open the downloaded .dmg file. Then run the "XcodeTools.mpkg" file and
complete the Xcode installation with all the default options.
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
(<control-click>) this sample PNG image to your "Desktop":
ShowTime.png
Then move the file into the "showtime" folder with the "mv"
command:
mv ../Desktop/ShowTime.png .
ls -l
Now we can create the icon file.
Steps:
- Use "Finder" to navigate into the "/Developer/Applications/Utilities"
folder and double-click "Icon Composer".
- Go back to "Finder" and navigate to your "showtime" folder (which
is in your home folder).
- Drag the "ShowTime.png" image file into the "128" box on the "Icon
Composer" screen.
- Go into the "File" menu and select the "Save" option.
- Click the down triangle button to show the file navigation options and
navigate to the "showtime" folder.
- Deselect the "Hide extension" option and save as "ShowTime.icns".
- Quit "IconComposer".
With our new icon, we can now create a Mac application.
8) Bundle the JAR
Using "Finder", navigate into the
"/Developer/Applications/Utilities" folder and double-click "Jar
Bundler".
Steps:
- For the "Main Class:", use the "Choose..." button and go to and choose
"ShowTime.jar".
- Check the "Use Macintosh Menu Bar" option.
- Use the "Choose Icon..." button to choose the "SnapBackup.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
"showtime" folder).
- Click the "Properties" tab and enter "1.0" into the "Version:" field.
- Also enter "1.0" into the "Get-Info String:" filed.
- Click the "Create Application..." button.
- Navigate into the "showtime" folder.
- In the "File:" field, enter "Show Time".
- Click the "Create" button.
- Quit "Jar Bundler".
We now have a proper Mac application. Next we'll create an installer for the
application.
9) Create Mac Installer
Using "Finder", navigate into the "/Developer/Applications/Utilities" folder and
double-click "PackageMaker".
Steps:
- In the "Organization:" field on the "Install Properties" window, enter
"com.centerkey". Then click "OK".
- Click on the "Configuration" tab, and in the "Title:" field enter "Show Time".
- Go to the "Project" menu and select "Add Contents...". Navigate to
"showtime" folder and then select and choose "Show Time".
- Deselect "Allow Relocation" (to prevent the build version from being confused
with the installed version).
- Click on the "Contents" tab. Check the "Include root in package" option
and click the "Apply Recommendations" button.
- Now click the "Build" (hammer) button. In the "Save As:" field,
enter "ShowTimeInstaller.pkg". Click the "Save" button and then the
"Return" button.
- Go to the "File" menu and select "Save". In the "Save As:" field,
enter "ShowTime.pmdoc" and then click "Save".
- Quit "PackageMaker".
Our 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 "showtime" folder.
Create a zip of "ShowTimeInstaller.pkg" using the "Compress" option on the
<control-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:
- Go to the "Apple" menu (
) and choose
"System Preferences...".
- In the "Internet & Wireless" section, click "Sharing".
- Check the option for "Web Sharing" service.
- 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 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 9, which creates the installer file (.pkg).
Wrap-Up
Here's the finished installer you can try out yourself:
For an example of how you might distribute your installer, take a look at:
If you want to add a "Visit Web Site" button to your application, check out:
That's it.
Comments or Questions
All the fields are optional. However, if you want a response, make
sure to provide your e-mail address.
Random
"Great tutorial." —
W.C., October 8, 2009
"Great tutorial! Many thanks, this is really helpful for a programming assignment in a computer network class I'm taking." —
Z.L., October 6, 2009
"Great tut omg thanks so much!" —
S., October 4, 2009
"I love this tutorial! Thank you!" —
S.R., March 4, 2009
"Fantastic!!!!! Thank you very muchhhhh!! I can start mac programing now. The tutorial is great!!!" —
F., November 8, 2008
"Incredibly clear to follow, Thanks very much" —
J.G., October 1, 2008
More...
"Très bon tutorial, merci beaucoup" —
K., November 7, 2007
"This is exactly the information I was looking for!! Great job explaining how to use
the Jar Bundler. I always wondered how to group my files into a single 'app'
file." —
J.L.M., July 6, 2007
"Thanks for the tutorial." —
D.W., June 6, 2007
"Great tutorial! Thank You very much!" —
T., May 19, 2007
"This is a very good developement friendly site" —
A.K., March 18, 2007
"Thankyou, I am very happy to read your content for MacJava. Thankyou very much" —
A., January 12, 2007
"Very Good!!!!" —
D.A., August 10, 2006
"Great article - I found it very helpful! Thanks!" —
J.T., August 1, 2006
"Hello, Great information, thank you very much! Can I translate your article in french?" —
P.T., January 21, 2006
"Great article!" —
D., December 9, 2005
"Thank you so much. Your instructions are perfectly concise. There is plenty of information about programming, but not nearly enough about installing and packaging. This page is a blessing." —
S.B., October 21, 2005
"This is a great info on mac application builder" —
S.S., October 11, 2005
"sweet article! one of the clearest and simplest ive seen and everything just works!!" —
J.B., September 9, 2005
"Amazing, I would never have thought it was that easy... You are truely the best!" —
M.F., September 7, 2005
"Couldn't be more clearer!" —
M.H., August 13, 2005
"THX a lot - very helpful and direct to the point" —
T.W., August 9, 2005
"Cool -- you solved the mystery!" —
C.M., June 27, 2005
 |
|
|