M'λ'
M'λ'

Reputation: 651

Embedding a JRE in a Swing application for Mac OS X

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?)?

Upvotes: 6

Views: 3515

Answers (3)

trashgod
trashgod

Reputation: 205785

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.

Upvotes: 2

Ian Roberts
Ian Roberts

Reputation: 122364

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.

Upvotes: 3

user330315
user330315

Reputation:

I think you can safely assume the JRE is already present on a Mac.

The best format (so I'm told - I'm not a Mac user myself) is a tar.gz (to preserve file attributes) of an "App" bundle.

You can use the "jarbundler" ( http://sourceforge.net/projects/jarbundler/ ) to create a proper Mac "bundle" that can be extracted without further ado. Make sure the Stub that is included in the bundle is marked as "executable" from within the Ant tar task.

I think there are some problems if more than one JRE is installed. In that case the correct Stub must be used to start the JRE version that your application needs. If I recall correctly this Stub is located at /System/Library/Frameworks/JavaVM.framework/Resources/MacOS/JavaApplicationStub

There is a page that indicates that there might be problems with Java7 and the jarbundler: http://informagen.com/JarBundler/

Upvotes: 1

Related Questions