![]() |
![]() |
|
![]() ![]()
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 JavaUsing "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. 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 FolderAt 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 CodeMac 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 ProgramBack 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 JARBefore 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: ShowTimeExit Pico and use the following "jar" command to create the "ShowTime.jar" file:
jar cmf MainClass.txt ShowTime.jar *.class
Now test your executable JAR with the following command:
ls -l
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.
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 XcodeApple's Xcode suite includes the developer tools you'll need to create an icon and an installer. Go to the Mac App Store and install Xcode. 7) Create Application IconThe 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:
With our new icon, we can now create a Mac application. 8) Bundle the JARBring 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:
We now have a proper Mac application. Next we'll create an installer for the application. 9) Create Mac InstallerUsing "Finder", navigate into the "/Developer/Applications/Utilities" folder and double-click "PackageMaker". Steps:
Our installer is done, but it's not yet download friendly. 10) Put Installer on a Web PageBefore 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:
Launch Safari and go to http://localhost/~you/download.html where "you" is your user name. Click the "ShowTimeInstaller.pkg.zip" link. If the install does not start automatically, go into your "Download" folder and double-click the file. 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. TroubleshootingIf 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-UpFor 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. |
||
![]() |
![]() |