Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

I've come across a nice tool in the Java 8 SDK (Java 7 has it too, but it has a different name) that lets you package your Java application as a native app for a range of platforms. This tool is called javapackager and it makes distribution of you Java apps so much easier than previously and best of all, it's part of the JDK itself.

Using the tool is fairly straight forward, it's what I use to package Mouslr, this is the command that I use after I've built all my code and built all the artefacts (I use IntelliJ for that purpose).
 javapackager command
javapackager -deploy \
-title "Mouslr" \
-name "Mouslr" \
-appclass net.igorkromin.mouslr.Capture \
-native dmg \
-outdir ~/Downloads \
-outfile Mouslr \
-srcdir "out/artifacts/Mouslr_jar"

I run javapackager in deploy mode and specify Mouslr as the title, name and the outfile parameters. The appclass is set to the main class of the application, this is the class with the main() method. I then tell javapackage to build a dmg native package, this is really the only option on OS X. The output directory is set to the Downloads folder and the source directory is set to the location where my jar files are saved.

There are a number of options that can be configured, including a custom Info.plist file, the DMG background image, app name, etc. All of this is set in a number of files that must be saved into the package/macosx directory, which must appear on the classpath to the javapackager tool. The easiest way to do this is to have this directory structure in your project directory and run javapackager from your project directory. In my case, my directory structure looks like this:

Now if you are wondering how you generate the icns files, Info.plist, etc, you need not worry. If you run javapackager and pass -verbose as a command line parameter, it will give you the location of where it saves its default configuration files, which you can simply copy into your project and modify.
 javapackager verbose output
Config files are saved to /var/folders/y2/__zvkpb547zg92kgv6rx5m540000gn/T/fxbundler3944182578574287884/macosx. Use them to customize package.

That's all there is to it. I've not done much customisation of my package apart from changing the DMG background, but the options are there. This tool will use the JRE that is the default system JRE i.e. the one that is running when you run the javapackager itself. This can be customised of course, but requires to use the Ant tasks that support this tool. I've not done this yet, but will be investigating this option some time in the future.

After the DMG file is created, it can be opened as usual. The packaged app can be dragged into the Applications folder and launched like any other app.


A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.