A BASH based JavaApplicationStub for Java Apps on Mac OS X that works with both Apple's and Oracle's plist format. It is released under the MIT License.
Whilst developing some Java apps for Mac OS X I was facing the problem of supporting two different Java versions – the "older" Apple versions and the "newer" Oracle versions.
Is there some difference, you might ask? Yes, there is!
- The installation directory differs:
- Apple Java 1.5/1.6:
/System/Library/Java/JavaVirtualMachines/ - Oracle JRE 1.7/1.8:
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/ - Oracle JDK 1.7/1.8:
/System/Library/Java/JavaVirtualMachines/
- Mac Apps built with tools designed for Apple's Java (like Apple's JarBundler or the OpenSource ANT task "Jarbundler") won't work on Macs with Oracle Java 7 and no Apple Java installed.
- This is because Apple's
JavaApplicationStubonly works for Apple's Java and their style to store Java properties in theInfo.plistfile. - To support Oracle Java 7 you would need to built a separate App package with Oracles ANT task "Appbundler".
- Thus you would need the user to know which Java distribution he has installed on his Mac. Not very user friendly...
-
Oracle uses a different syntax to store Java properties in the applications
Info.plistfile. A Java app packaged as a Mac app with Oracles Appbundler also needs a differentJavaApplicationStuband therefore won't work on systems with Apple's Java... -
Starting with Mac OS X 10.10 (Yosemite), Java Apps won't open anymore if they contain the deprecated Plist dictionary
Java. This isn't confirmed by Apple, but issue #9 leads to this assumption:
- Apple seems to declare the
Javadictionary as deprecated and ties it to the old Apple Java 6. If you have a newer version installed the app won't open. - If Java 7/8 is installed, Apple doesn't accept those java versions as suitable
- Apple prompts for JRE 6 download even before the
JavaApplicationStubis executed. This is why we can't intercept at this level and need to replace theJavadictionary by aJavaXdictionary key. - This requires to use the latest JarBundler version (see below for more details)
So why, oh why, couldn't Oracle just use the old style of storing Java properties in Info.plist and offer a universal JavaApplicationStub?! 😡
Well, since I can't write such a script in C, C# or whatever fancy language, I wrote it as a Shell script. And it works!
You don't need a native JavaApplicationStub file anymore. The Shell script needs to be executable – that's all.
The script reads JVM properties from Info.plist regardless of whether it's Apple or Oracle flavour and feeds it to a commandline java call like the following:
# execute Java and set
# - classpath
# - dock icon
# - application name
# - JVM options
# - JVM default options
# - main class
# - JVM arguments
exec "$JAVACMD" \
-cp "${JVMClassPath}" \
-splash:"${ResourcesFolder}/${JVMSplashFile}" \
-Xdock:icon="${ResourcesFolder}/${CFBundleIconFile}" \
-Xdock:name="${CFBundleName}" \
${JVMOptions:+$JVMOptions }\
${JVMDefaultOptions:+$JVMDefaultOptions }\
${JVMMainClass}\
${JVMArguments:+ $JVMArguments}\
${ArgsPassthru:+ $ArgsPassthru}It sets the classpath, the dock icon, the AboutMenuName (in Xdock style) and then every JVMOptions, JVMDefaultOptions or JVMArguments found in the Info.plist file.
The WorkingDirectory is either retrieved from Apple's Plist key Java/WorkingDirectory or set to the JavaRoot directory within the app bundle.
The name of the main class is also retrieved from Info.plist. If no main class is found, an applescript error dialog is shown and the script exits with exit code 1.
There is some foo happening to determine which Java versions are installed – here's the list in which order system properties are checked:
- System variable
$JAVA_HOME
- can also be set to a relative path using the
<LSEnvironment>Plist dictionary key- which allows for bundling a custom version of Java inside your app!
- Highest available Java version (Java 8 trumps 7) found in one of these locations:
/usr/libexec/java_homesymlinks- Oracle's JRE Plugin:
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java - Symlink for old Apple Java:
/Library/Java/Home/bin/java
- If you require a specific Java version with the Plist key
JVMVersionthe script will try to find a matching JDK or JRE in all of the above locations
- if multiple matching JVM's are found, the script will pick the latest (highest version number)
If none of these could be found or executed the script shows an applescript error dialog saying that Java needs to be installed:
Messages are localized and displayed either in English (Default), French or German. Language contributions are very welcome!
Use whichever ANT task you like:
- the opensource "JarBundler" (recommended)
- or my JarBundler fork (deprecated)
- both support the newly introduced and recommended
JavaXdict key
- Oracle's opensource "Appbundler" (seems to be dead)
Download the latest JarBundler release from its github repo.
❗ Attention:
Using an older version of JarBundler (e.g. old JarBundler ≤ v2.3 or new JarBundler ≤ v3.2) might result in issue #9 (Mac OS X 10.10 asking to install deprecated Apple JRE 6 instead of using a newer Java version)
If you don't want to care about compatibility issues between OS X and Java versions, make sure to use the latest JarBundler version ≥ 3.3
Then place the universalJavaApplicationStub from this repo in your build resources folder and link it in your ANT task (attribute stubfile). Don't forget to set the newly introduced useJavaXKey option for compatibility:
<jarbundler
name="Your-App"
shortname="Your Application"
icon="${resources.dir}/icon.icns"
stubfile="${resources.dir}/universalJavaApplicationStub"
useJavaXKey="true"
... >
</jarbundler>The ANT task will care about the rest...
You should get a fully functional Mac Application Bundle working with both Java distributions from Apple and Oracle and all Mac OS X versions.
Just place the universalJavaApplicationStub from this repo in your build resources folder and link it in your ANT task (attribute executableName from infinitekind fork):
<appbundler
name="Your-App"
displayname="Your Application"
icon="${resources.dir}/icon.icns"
executableName="${resources.dir}/universalJavaApplicationStub"
... >
</appbundler>The ANT task will care about the rest...
You should get a fully functional Mac Application Bundle working with both Java distributions from Apple and Oracle and all Mac OS X versions.
At the moment, there's no support for
- required JVM architecture (like
x86_64, etc.)
universalJavaApplicationStub is released under the MIT License.
