A BASH based launcher stub for Java based macOS Apps that works with both Apple's and Oracle's plist format. It is released under the MIT License.
π π See the CHANGELOG for a Release History and feature details.
π π₯ Report Issues at the GitHub Issues Page.
π€ β If you have a general question about how a feature works or would like to share an idea or a usecase for this project, then please use the GitHub Discussions Page.
Whilst developing some Java Apps for Mac OS X I was facing the problem of supporting two different kinds of Java versions β the old Apple versions and the new Oracle versions.
Is there some difference, you might ask? Yes, there is!
The installation directory differs:
/System/Library/Java/JavaVirtualMachines/
or /Library/Java/Home/bin/java
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/
/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.
JavaApplicationStub
only works for Apple's Java and their style to store Java properties in the Info.plist
file.Oracle uses a different syntax to store Java properties in the applications Info.plist
file. A Java Application packaged as a Mac App with Oracle's Appbundler also needs a different JavaApplicationStub
and therefore won't work on systems with Apple's old 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:
Java
dictionary as deprecated and ties it to their old Apple Java 6. If you have a newer Oracle Java version installed the app won't open.JavaApplicationStub
is executed. This is why we can't intercept at this level and need to replace the Java
dictionary by a JavaX
dictionary key.TL;DR: Since there is no universally working JavaApplicationStub for Java 6, 7 and above, and because Apple and Oracle really screwed things up during their Java transition phase, I was in need of a new Stub file. And well, since I can't write such a script in C, C# or whatever fancy language, I wrote it as a Bash script. And it works! The original script was inspired by Ian Roberts stackoverflow answer. Thanks, Ian!
You don't need a native JavaApplicationStub
file anymore. The Bash script needs to be executable β that's all.
The script reads JVM properties from Info.plist
regardless of whether it's Apple or Oracle syntax and passes them to a exec java
call like the following simplified:
# execute Java and set
# - classpath
# - splash image
# - dock icon
# - app name
# - JVM options / properties (-D)
# - JVM default options (-X)
# - main class
# - main class arguments
# - passthrough arguments from Terminal or Drag'n'Drop to Finder icon
exec "${JAVACMD}" \
-cp "${JVMClassPath}" \
-splash:"${ResourcesFolder}/${JVMSplashFile}" \
-Xdock:icon="${ResourcesFolder}/${CFBundleIconFile}" \
-Xdock:name="${CFBundleName}" \
${JVMOptions} \
${JVMDefaultOptions} \
${JVMMainClass} \
${MainArgsArr} \
${ArgsPassthru}
It sets the classpath, the dock icon, the AboutMenuName (as Xdock parameter) and then every JVMOptions, JVMDefaultOptions or JVMArguments found in the Info.plist
file. See the table below for more supported Plist keys.
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:
$JAVA_HOME
<LSEnvironment>
Plist dictionary key/usr/libexec/java_home
symlinks/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
/Library/Java/Home/bin/java
JVMVersion
the script will try to find a matching JDK or JRE in all of the above locations
JVMVersion
to specify a max requirement. See issue #51 for examples.If none of these can 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, German or Chinese. Language contributions are very welcome! Thank you!
Use whichever ANT task you like:
JavaX
dict keyOr build the App bundle statically from scratch...
Download the latest JarBundler release from its github repo.
:exclamation: 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 take care of all the rest... But of course you can specify more options. Please check the JarBundler docs.
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 take care of all the rest... But of course you can specify more options. Please check the Appbundler docs.
You should get a fully functional Mac Application Bundle working with both Java distributions from Apple and Oracle and all Mac OS X versions.
Function | Apple PList key | Oracle PList key |
---|---|---|
App Name (Dock Name) | :CFBundleName |
:CFBundleName |
App Icon (Dock Icon) | :CFBundleIconFile |
:CFBundleIconFile |
Working Directory π | :Java(X):WorkingDirectory fallback to name.app/ | *not supported*<br/>default: name.app/Contents/Java/` |
|
Java Min/Maxβ Version Requirement | :Java(X):JVMVersion |
:JVMVersion |
Java ClassPath (-cp β¦ ) π |
:Java(X):ClassPath |
:JVMClassPath |
Java Main Class | :Java(X):MainClass |
:JVMMainClassName |
Splash Image (-splash:β¦ ) |
:Java(X):SplashFile |
:JVMSplashFile |
Java VM Options (-Xβ¦ ) π |
:Java(X):VMOptions |
:JVMDefaultOptions |
-XstartOnFirstThread β
|
:Java(X):StartOnMainThread |
not supported |
Java Properties (-Dβ¦ ) π |
:Java(X):Properties |
:JVMOptions |
Main Class Arguments π | :Java(X):Arguments |
:JVMArguments |
π Variable placeholders $APP_PACKAGE
, $APP_ROOT
, $JAVAROOT
, $USER_HOME
get expanded in these contexts.
Since v3.0 (#51)
Use Java(X):JVMVersion
(Apple style) or :JVMVersion
(Oracle style) with the following values:
1.8
or 1.8*
for Java 81.8+
for Java 8 or higher1.7;1.8*
for Java 7 or 81.8;9.0
for Java 8 up to exactly 9.0 (but not 9.0.)1.8;9.0*
for Java 8 and 9.0. but not 9.1.*You can use the Plist key LSEnvironment
to export and set the $JAVA_HOME
environment variable relative to your App's root directory:
<key>LSEnvironment</key>
<dict>
<key>JAVA_HOME</key>
<string>Contents/Frameworks/jdk8u232-b09-jre/Contents/Home</string>
<dict>
Starting with macOS 10.15 Apple by default prevents access to Protected Resources like the user's Download, Documents or Desktop folders and shows a security dialog which the user has to accept before access is granted.
When using javax.swing.JFileChooser
in your application, which supports these kinds of security dialogs (interestingly java.awt.FileDialog
does not!), you should use a compiled binary of the universalJavaApplicationStub
script instead of the plain bash script. See issue #85 for more details.
Starting with version 3.1.0 we provide pre-built binaries on the Releases page which are automatically compiled with shc
via GitHub Actions CI.
Additionaly we recommend you set Usage Description Plist keys as described further below.
NSAppleEventsUsageDescription
Starting with Mac OS 10.14 users may be confronted with an additional system security dialog before any warning dialog of this stub is shown. See issue #77 for more details.
This happens because the warning dialogs of this launcher stub are displayed with AppleScript.
It's recommended to at least set the following Plist key in order to display a descriptive message to the user, why he should grant the app system access:
<key>NSAppleEventsUsageDescription</key>
<string>There was an error while launching the application. Please click OK to display a dialog with more information or cancel and view the syslog for details.</string>
If your app requires access to Protected Resources like the user's Download, Documents or Desktop folders, there are a couple more properties to add in your Plist file for setting a Usage Description:
NSDownloadsFolderUsageDescription
NSDocumentsFolderUsageDescription
NSDesktopFolderUsageDescription
This may be extra important when using javax.swing.JFileChooser
in your application. See issue #85 for more details.
Starting with version 3.0 universalJavaApplicationStub
logs data to the syslog
facility which can be easily accessed with the Console.app
utility by searching for syslog:
Log data includes debug information of the JVM search strategy, App name, language, selected JVM, WorkingDirectory and exec call.
At the moment, there's no support for
x86_64
, etc.)universalJavaApplicationStub is released under the MIT License.