How to create Mac OS app bundle for Java apps

Java apps work great on our Macs, JAR bundles are recognized “out of the box” and launched as executable files automatically by the operating system. But we always want more and we want to be as native as possible so naturally we would like to package our JAR’s into .app bundles to give them more Mac look & feel. In this post I’ll explain how to easily create Mac app bundles out of your Java app JAR’s using Mac OS development tools.

Preps

We’ll need a few things to begin with and those are the JAR file(s) you’re bundling and the app icons. Icons need to be in Mac OS .icns format and there’s a nice tool that will create this for you and it’s called “Icon Composer”. Icon composer (as well as other tools I’ll mention in this post) is installed along with Mac OS development tools from Apple’s website.

Using Icon Composer to create .icns is pretty straight-forward process. You will just need a 512×512 PNG of your icon and you just drag & drop it into the Icon Composer and save as .icns.

Icon Composer

Icon Composer

Bundling Mac OS app

The thing to do is to actually create a Mac OS .app bundle from you JAR file(s) using “JAR bundler” tool (comes along with Mac OS development tools). The app interface is simple enough – you get to choose your JAR executable and specify main class and you’re good to go. There’s, of course, more options if you want to include additional jar’s or files and to specify basic properties for your Java app.

JAR bundler

JAR bundler

Now if you have a single JAR file you shouldn’t have any problems. However, if your JAR file depends on some external libraries, images etc. you might have troubles with dependency file paths. I did. I had a subfolders “lib” and “images” full of external libraries and images my app was using. I tried to solve it ‘their’ way by trying to fix the paths but it took a long time and I couldn’t get it working so I found a much quicker workaround I want to share here.

Instead of having multiple JAR files and additional dependencies, I decided to end the nightmare of handling file paths by bundling everything into one single JAR file. And directly from NetBeans too. There’s a well documented tutorial here. Basically, you need to put the code below in your build.xml file and choose target package-for-store when building your app and a single JAR will be created for you.

<target name="package-for-store" depends="jar">
 
        <!-- Change the value of this property to be the name of your JAR,
             minus the .jar extension. It should not have spaces.
             <property name="store.jar.name" value="MyJarName"/>
        -->
        <property name="store.jar.name" value="MyJarName"/>
 
        <!-- don't edit below this line -->
 
        <property name="store.dir" value="store"/>
        <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
 
        <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
 
        <delete dir="${store.dir}"/>
        <mkdir dir="${store.dir}"/>
 
        <jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
            <zipgroupfileset dir="dist" includes="*.jar"/>
            <zipgroupfileset dir="dist/lib" includes="*.jar"/>
 
            <manifest>
                <attribute name="Main-Class" value="${main.class}"/>
            </manifest>
        </jar>
 
        <zip destfile="${store.jar}">
            <zipfileset src="${store.dir}/temp_final.jar"
            excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
        </zip>
 
        <delete file="${store.dir}/temp_final.jar"/>
 
    </target>

Now all you need to do is to use this JAR file with JAR bundler and you’ll have a Mac OS .app in no time ready to send to your users.

In the next post, I’ll write how to use this Mac OS .app to create an installer for your app. Until then, stay tuned!

One thought on “How to create Mac OS app bundle for Java apps

  1. Pingback: How to make pkg installer for Mac apps | Development

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>