mac java

How to create a macOS installer for a Java application (.jar)

(Updated for macOS 14 Sonoma)

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

It's easy to turn your Java Swing program (.jar) into a proper macOS 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.

installer
Screenshot: ShowTime-1.0.pkg

This step-by-step tutorial is appropriate for beginner level developers.
(For the truly impatient, you can even run through the whole
tutorial in 30 seconds )

1) Launch Unix terminal

Using Finder, go into "Applications" and then open the "Utilities" folder.

Now open Terminal and you'll be at the Unix prompt.

2) Install Homebrew and Java (openjdk)

Homebrew is a widely used open-source software package management system for Apple macOS that makes is easy to install Java (openjdk).

Enter the following commands to install Homebrew and Java (openjdk):

         url=https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh
         /bin/bash -c "$(curl -fsSL $url)"
         brew install openjdk
         java --version
      

Verify you are running openjdk 17.0.0 or later.

If your Mac has a prior version of Java already installed, consulte the openjdk formula documentation for instructions on creating a symlink to point macOS to the new openjdk instalation.

3) 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.

4) Write some Java code

Get the sample Java code and take a look at the Swing commands that create a simple window for displaying the current time:

         curl --remote-name https://centerkey.com/mac/java/ShowTime.java
         cat ShowTime.java
      

You'll see the code:

ShowTime.java

            import java.util.Date;
            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(new Date().toString()));
                  frame.pack();
                  frame.setVisible(true);
                  }

            }
         

5) Compile Java program

Back at the Unix prompt, compile the Java program into a class file:

         javac --version
         javac ShowTime.java
         ls -o
      

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.

6) Make executable JAR

Before we make an executable JAR file, we need a manifest file to indicate which class contains the "main" function.  Create the file:

         echo "Main-Class: ShowTime" > MainClass.txt
         cat MainClass.txt
      

The resulting manifest file contains a single line:

MainClass.txt

            Main-Class: ShowTime
         

Now use the following jar command to create the "ShowTime.jar" file:

         jar --version
         jar cmfv MainClass.txt ShowTime.jar *.class
         ls -o
      

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 your screen.

Click the red button (marble) to exit the program.

Note:

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

7) Create installer background and 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.

Use the following commands to download a sample PNG image, resize the image to appropriate dimensions for an icon, and convert it into the .icns format:

         curl --remote-name https://centerkey.com/mac/java/ShowTime.png
         sips --version
         sips --resampleHeightWidth 120 120 --padToHeightWidth 175 175 \
            ShowTime.png --out ShowTime-background.png
         cp -v ShowTime-background.png ShowTime-background-darkAqua.png
         mkdir ShowTime.iconset
         sips --resampleHeightWidth 128 128 \
            ShowTime.png --out ShowTime.iconset/icon_128x128.png
         iconutil --convert icns ShowTime.iconset
         ls -o
      

8) Create package resource folder

Use the following commands to copy the graphic files into a folder for package resources:

         mkdir -p package/macos
         cp -v *.png *.icns package/macos
      

9) Build macOS application and installer

Run the jpackage tool to build the ShowTime.app appplication and wrap it in into an installer:

         jpackage --version
         jpackage --name ShowTime --input . --main-jar ShowTime.jar \
            --resource-dir package/macos --type pkg
         ls -o *.pkg
      
Note 1:

The executable JAR file ShowTime.jar checks in at a mere 0.001 MB, but the installer file ShowTime-1.0.pkg almost hits a whopping 50 MB.  The reason is that the installer bundles the JRE (an Apple requirement for publishing Java programs to the Mac App Store).

Note 2:

When you are ready to distribute to the public, you'll want to obtain a Developer ID certificate and then sign your installer with it.

10) Try it out

Run the installer:

         open ShowTime-1.0.pkg
      

After completing the installation, go into the "Applications" folder and open the "ShowTime" application.

Wrap-Up

If you run into any problems, compare your terminal commands and output to:

Output log

For an example of how you might distribute your installer, take a look at:

Snap Backup

That's it.

Comments or Questions

Send us a message

Powered by PERFECT

All the fields are optional.  However, if you want a response, make sure to provide your email address.

Random

"Very nice tutorial."J.H., September 18, 2018

"Hi! Thanks for a great tutorial."R.D., April 4, 2018

"Super good tutorial to make a hard job simple."G., April 23, 2014

"Great Tutorial. Didn't expect that 'write some java, compile and run it' can be so easy."C.S., October 16, 2013

"Fantastic! Much appreciated =)"T.B., April 15, 2013

"It just works! Thanks!"J.G., January 18, 2012

"Thank you for your "Mac Java!" tutorial on bundling a JAR and making an installer.  It couldn't be any better!!!"Anonymous, October 20, 2011

"ce que je cherchais, merci"A.M., May 3, 2010

More...

"Great tutorial.  Thank you for concise directions to get me running with Java on my new iMac."A.M., May 3, 2010

"Thank you so much for this tutorial! I am new to programming and have never written any code on my mac before now. This was very helpful!"M.A., August 29, 2010

"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

"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