![]() |
![]() |
|||
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
"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.Icons ↓ 1) Install XcodeBefore 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 Terminal3) Make Project Folder
mkdir ItsShowtime
The first command creates a folder called "ItsShowtime", and the second command
moves you into the new folder.cd ItsShowtime 4) Write Some Java Code
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 <ctrl-x> to exit Pico. 5) Compile Java Program
javac ShowTime.java
We could run the class file directly, but a class file is cumbersome.
Instead we will create an executable JAR file.ls -la 6) Make Executable JAR
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 -la
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 IconDownload 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:
8) Bundle the JARSteps:
9) Create Mac InstallerSteps:
10) Put Installer on a Web PageBack 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 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. TroubleshootingIf 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-UpHere'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.
|
||||
![]() |
![]() |