Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding a JRE in a Swing application for Mac OS X

Tags:

java

macos

swing

I must ship a swing application with an embedded JRE. A zipped archive with application + JRE + .bat/.sh did the trick for Windows and Linux. Users download the zip, unzip it, and launch the application. Perfect.

But now, I must provide the same thing for Mac OS X. I have read / was told a lot of different things on that matter (forbidden to distribute a JRE on Mac, the contrary, there is always a JRE on Mac, etc...), so I'm really confused about what I can possibly do.

Does anybody already did such a thing? How did you solved the Mac application deployment? As a bonus, what is the best format to distribute my application in Mac (zip?)?

like image 597
M'λ' Avatar asked Jan 23 '13 14:01

M'λ'


2 Answers

Have a look at the appbundler project on java.net. It provides an Ant task that will package your application up as a normal Mac .app bundle, and can optionally include an embedded JRE.

The whole Java landscape on Mac is a bit of a mess at the moment as we're still in the transitional period where Java 6 releases are supplied and maintained by Apple and Java 7 releases come direct from Oracle. The jarbundler project referred to by a_horse_with_no_name is for wrapping up a JAR as a .app bundle that will run on the Apple-supplied Java 6 that is included with Mac OS X 10.5/6/7, but not on Oracle Java 7, conversely appbundler targets Oracle Java 7 and its .app bundles won't run on Java 6.

If you want to target recent Macs running 10.7 or 10.8, and in particular if you want to distribute your app via the Mac App Store, then you should use appbundler and bundle a copy of the JRE. If you don't want to distribute via the store then the embedded JRE is optional. If your app can run on Java 6 then targetting Apple Java 6 with jarbundler will mean your app can run on older (<= 10.6) Macs. But then anyone with a more recent Mac that has only Java 7 will be prompted to download and install Java 6 when they try and run your app.

It's fine to distribute the .app in a .zip archive, as long as everything in the Contents/MacOS directory (and the appropriate files in the embedded JRE if applicable) inside the app is marked with execute permission in the zip file. If you're building with Ant you'll need to use <zipfileset>s with the right filemode.

like image 197
Ian Roberts Avatar answered Oct 05 '22 14:10

Ian Roberts


The easiest approach uses the same JAR as any other platform, deployed via Java Web Start. I've never tried using it to install a JRE.

The best approach to provide a more Mac friendly experience requires you to wrap your JAR(s) in an application bundle, discussed in the article's cited here. The preferred delivery format is a compressed disk image, .dmg.

The article Java Deployment Options for Mac OS X compares both options. This game is an example that can be launched either way.

Addendum: For the curious, the Mac OS X Finder treats .jnlp files as documents, opened with the JWS launcher by default. I keep a folder of some frequently used .jnlp files in a dock folder for quick access. The Java Preferences control panel permits managing cached applications. Choosing to install a shortcut creates a simple Mac Application bundle in the destination folder, and the included Info.plist can be edited just like any other. The decision to customize the .plist hinges on what features the application needs: screen menu bar, dock name and icon, JNI path, etc.

like image 32
trashgod Avatar answered Oct 05 '22 13:10

trashgod