2 ##################################################################################
4 # universalJavaApplicationStub #
6 # A BASH based JavaApplicationStub for Java Apps on Mac OS X #
7 # that works with both Apple's and Oracle's plist format. #
9 # Inspired by Ian Roberts stackoverflow answer #
10 # at http://stackoverflow.com/a/17546508/1128689 #
12 # @author Tobias Fischer #
13 # @url https://github.com/tofi86/universalJavaApplicationStub #
17 ##################################################################################
19 # The MIT License (MIT) #
21 # Copyright (c) 2014-2021 Tobias Fischer #
23 # Permission is hereby granted, free of charge, to any person obtaining a copy #
24 # of this software and associated documentation files (the "Software"), to deal #
25 # in the Software without restriction, including without limitation the rights #
26 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
27 # copies of the Software, and to permit persons to whom the Software is #
28 # furnished to do so, subject to the following conditions: #
30 # The above copyright notice and this permission notice shall be included in all #
31 # copies or substantial portions of the Software. #
33 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
34 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
35 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
36 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
37 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
38 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
41 ##################################################################################
45 # function 'stub_logger()'
47 # A logger which logs to the macOS Console.app using the 'syslog' command
49 # @param1 the log message
51 ################################################################################
52 function stub_logger() {
54 Facility com.apple.console \
56 Sender "$(basename "$0")" \
57 Message "[$$][${CFBundleName:-$(basename "$0")}] $1"
62 # set the directory abspath of the current
63 # shell script with symlinks being resolved
64 ############################################
67 while [ -h "$PRG" ]; do
69 link=$(expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null)
70 if expr "$link" : '^/' 2> /dev/null >/dev/null; then
73 PRG="$(dirname "$PRG")/$link"
76 PROGDIR=$(dirname "$PRG")
77 stub_logger "[StubDir] $PROGDIR"
81 # set files and folders
82 ############################################
84 # the absolute path of the app package
85 cd "$PROGDIR"/../../ || exit 11
86 AppPackageFolder=$(pwd)
88 # the base path of the app package
92 # set Apple's Java folder
93 AppleJavaFolder="${AppPackageFolder}"/Contents/Resources/Java
95 # set Apple's Resources folder
96 AppleResourcesFolder="${AppPackageFolder}"/Contents/Resources
98 # set Oracle's Java folder
99 OracleJavaFolder="${AppPackageFolder}"/Contents/Java
101 # set Oracle's Resources folder
102 OracleResourcesFolder="${AppPackageFolder}"/Contents/Resources
104 # set path to Info.plist in bundle
105 InfoPlistFile="${AppPackageFolder}"/Contents/Info.plist
107 # set the default JVM Version to a null string
113 # function 'plist_get()'
115 # read a specific Plist key with 'PlistBuddy' utility
117 # @param1 the Plist key with leading colon ':'
118 # @return the value as String or Array
119 ################################################################################
121 /usr/libexec/PlistBuddy -c "print $1" "${InfoPlistFile}" 2> /dev/null
124 # function 'plist_get_java()'
126 # read a specific Plist key with 'PlistBuddy' utility
127 # in the 'Java' or 'JavaX' dictionary (<dict>)
129 # @param1 the Plist :Java(X):Key with leading colon ':'
130 # @return the value as String or Array
131 ################################################################################
133 plist_get ${JavaKey:-":Java"}$1
138 # read Info.plist and extract JVM options
139 ############################################
141 # read the program name from CFBundleName
142 CFBundleName=$(plist_get ':CFBundleName')
144 # read the icon file name
145 CFBundleIconFile=$(plist_get ':CFBundleIconFile')
148 # check Info.plist for Apple style Java keys -> if key :Java is present, parse in apple mode
149 /usr/libexec/PlistBuddy -c "print :Java" "${InfoPlistFile}" > /dev/null 2>&1
153 # if no :Java key is present, check Info.plist for universalJavaApplication style JavaX keys -> if key :JavaX is present, parse in apple mode
154 if [ $exitcode -ne 0 ]; then
155 /usr/libexec/PlistBuddy -c "print :JavaX" "${InfoPlistFile}" > /dev/null 2>&1
161 # read 'Info.plist' file in Apple style if exit code returns 0 (true, ':Java' key is present)
162 if [ $exitcode -eq 0 ]; then
163 stub_logger "[PlistStyle] Apple"
165 # set Java and Resources folder
166 JavaFolder="${AppleJavaFolder}"
167 ResourcesFolder="${AppleResourcesFolder}"
169 # set expandable variables
170 APP_ROOT="${AppPackageFolder}"
171 APP_PACKAGE="${AppPackageFolder}"
172 JAVAROOT="${AppleJavaFolder}"
176 # read the Java WorkingDirectory
177 JVMWorkDir=$(plist_get_java ':WorkingDirectory' | xargs)
178 # set Working Directory based upon PList value
179 if [[ ! -z ${JVMWorkDir} ]]; then
180 WorkingDirectory="${JVMWorkDir}"
182 # AppPackageRoot is the standard WorkingDirectory when the script is started
183 WorkingDirectory="${AppPackageRoot}"
185 # expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
186 WorkingDirectory=$(eval echo "${WorkingDirectory}")
189 # read the MainClass name
190 JVMMainClass="$(plist_get_java ':MainClass')"
192 # read the SplashFile name
193 JVMSplashFile=$(plist_get_java ':SplashFile')
195 # read the JVM Properties as an array and retain spaces
197 JVMOptions=($(xargs -n1 <<<$(plist_get_java ':Properties' | grep " =" | sed 's/^ */-D/g' | sed -E 's/ = (.*)$/="\1"/g')))
199 # post processing of the array follows further below...
201 # read the ClassPath in either Array or String style
202 JVMClassPath_RAW=$(plist_get_java ':ClassPath' | xargs)
203 if [[ $JVMClassPath_RAW == *Array* ]] ; then
204 JVMClassPath=.$(plist_get_java ':ClassPath' | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs)
206 JVMClassPath=${JVMClassPath_RAW}
208 # expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
209 JVMClassPath=$(eval echo "${JVMClassPath}")
211 # read the JVM Options in either Array or String style
212 JVMDefaultOptions_RAW=$(plist_get_java ':VMOptions' | xargs)
213 if [[ $JVMDefaultOptions_RAW == *Array* ]] ; then
214 JVMDefaultOptions=$(plist_get_java ':VMOptions' | grep " " | sed 's/^ */ /g' | tr -d '\n' | xargs)
216 JVMDefaultOptions=${JVMDefaultOptions_RAW}
218 # expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME (#84)
219 JVMDefaultOptions=$(eval echo "${JVMDefaultOptions}")
221 # read StartOnMainThread and add as -XstartOnFirstThread
222 JVMStartOnMainThread=$(plist_get_java ':StartOnMainThread')
223 if [ "${JVMStartOnMainThread}" == "true" ]; then
224 JVMDefaultOptions+=" -XstartOnFirstThread"
227 # read the JVM Arguments in either Array or String style (#76) and retain spaces
229 MainArgs_RAW=$(plist_get_java ':Arguments' | xargs)
230 if [[ $MainArgs_RAW == *Array* ]] ; then
231 MainArgs=($(xargs -n1 <<<$(plist_get_java ':Arguments' | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/ */ /g')))
233 MainArgs=($(xargs -n1 <<<$(plist_get_java ':Arguments')))
236 # post processing of the array follows further below...
238 # read the Java version we want to find
239 JVMVersion=$(plist_get_java ':JVMVersion' | xargs)
240 # post processing of the version string follows below...
243 # read 'Info.plist' file in Oracle style
245 stub_logger "[PlistStyle] Oracle"
247 # set Working Directory and Java and Resources folder
248 JavaFolder="${OracleJavaFolder}"
249 ResourcesFolder="${OracleResourcesFolder}"
250 WorkingDirectory="${OracleJavaFolder}"
252 # set expandable variables
253 APP_ROOT="${AppPackageFolder}"
254 APP_PACKAGE="${AppPackageFolder}"
255 JAVAROOT="${OracleJavaFolder}"
258 # read the MainClass name
259 JVMMainClass="$(plist_get ':JVMMainClassName')"
261 # read the SplashFile name
262 JVMSplashFile=$(plist_get ':JVMSplashFile')
264 # read the JVM Options as an array and retain spaces
266 JVMOptions=($(plist_get ':JVMOptions' | grep " " | sed 's/^ *//g'))
268 # post processing of the array follows further below...
270 # read the ClassPath in either Array or String style
271 JVMClassPath_RAW=$(plist_get ':JVMClassPath')
272 if [[ $JVMClassPath_RAW == *Array* ]] ; then
273 JVMClassPath=.$(plist_get ':JVMClassPath' | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs)
274 # expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
275 JVMClassPath=$(eval echo "${JVMClassPath}")
277 elif [[ ! -z ${JVMClassPath_RAW} ]] ; then
278 JVMClassPath=${JVMClassPath_RAW}
279 # expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
280 JVMClassPath=$(eval echo "${JVMClassPath}")
283 #default: fallback to OracleJavaFolder
284 JVMClassPath="${JavaFolder}/*"
285 # Do NOT expand the default 'AppName.app/Contents/Java/*' classpath (#42)
288 # read the JVM Default Options by parsing the :JVMDefaultOptions <dict>
289 # and pulling all <string> values starting with a dash (-)
290 JVMDefaultOptions=$(plist_get ':JVMDefaultOptions' | grep -o " \-.*" | tr -d '\n' | xargs)
291 # expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME (#99)
292 JVMDefaultOptions=$(eval echo "${JVMDefaultOptions}")
294 # read the Main Arguments from JVMArguments key as an array and retain spaces (see #46 for naming details)
296 MainArgs=($(xargs -n1 <<<$(plist_get ':JVMArguments' | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/ */ /g')))
298 # post processing of the array follows further below...
300 # read the Java version we want to find
301 JVMVersion=$(plist_get ':JVMVersion' | xargs)
302 # post processing of the version string follows below...
306 # (#75) check for undefined icons or icon names without .icns extension and prepare
307 # an osascript statement for those cases when the icon can be shown in the dialog
309 if [ ! -z ${CFBundleIconFile} ]; then
310 if [[ ${CFBundleIconFile} == *.icns ]] && [[ -f "${ResourcesFolder}/${CFBundleIconFile}" ]] ; then
311 DialogWithIcon=" with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
312 elif [[ ${CFBundleIconFile} != *.icns ]] && [[ -f "${ResourcesFolder}/${CFBundleIconFile}.icns" ]] ; then
313 CFBundleIconFile+=".icns"
314 DialogWithIcon=" with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
319 # JVMVersion: post processing and optional splitting
320 if [[ ${JVMVersion} == *";"* ]]; then
321 minMaxArray=(${JVMVersion//;/ })
322 JVMVersion=${minMaxArray[0]//+}
323 JVMMaxVersion=${minMaxArray[1]//+}
325 stub_logger "[JavaRequirement] JVM minimum version: ${JVMVersion}"
326 stub_logger "[JavaRequirement] JVM maximum version: ${JVMMaxVersion}"
328 # MainArgs: expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
330 for i in "${MainArgs[@]}"
332 MainArgsArr+=("$(eval echo "$i")")
335 # JVMOptions: expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
337 for i in "${JVMOptions[@]}"
339 JVMOptionsArr+=("$(eval echo "$i")")
343 # internationalized messages
344 ############################################
346 # supported languages / available translations
347 stubLanguages="^(fr|de|zh|es|en)-"
349 # read user preferred languages as defined in macOS System Preferences (#101)
350 stub_logger '[LanguageSearch] Checking preferred languages in macOS System Preferences...'
351 appleLanguages=($(defaults read -g AppleLanguages | grep '\s"' | tr -d ',' | xargs))
352 stub_logger "[LanguageSearch] ... found [${appleLanguages[*]}]"
355 for i in "${appleLanguages[@]}"
358 if [[ "$i" =~ $stubLanguages ]]; then
359 stub_logger "[LanguageSearch] ... selected '$i' ('$langValue') as the default language for the launcher stub"
360 language=${langValue}
364 if [ -z "${language}" ]; then
366 stub_logger "[LanguageSearch] ... selected fallback 'en' as the default language for the launcher stub"
368 stub_logger "[Language] $language"
371 case "${language}" in
374 MSG_ERROR_LAUNCHING="ERREUR au lancement de '${CFBundleName}'."
375 MSG_MISSING_MAINCLASS="'MainClass' n'est pas spécifié.\nL'application Java ne peut pas être lancée."
376 MSG_JVMVERSION_REQ_INVALID="La syntaxe de la version de Java demandée est invalide: %s\nVeuillez contacter le développeur de l'application."
377 MSG_NO_SUITABLE_JAVA="La version de Java installée sur votre système ne convient pas.\nCe programme nécessite Java %s"
378 MSG_JAVA_VERSION_OR_LATER="ou ultérieur"
379 MSG_JAVA_VERSION_LATEST="(dernière mise à jour)"
380 MSG_JAVA_VERSION_MAX="à %s"
381 MSG_NO_SUITABLE_JAVA_CHECK="Merci de bien vouloir installer la version de Java requise."
382 MSG_INSTALL_JAVA="Java doit être installé sur votre système.\nRendez-vous sur java.com et suivez les instructions d'installation..."
383 MSG_LATER="Plus tard"
384 MSG_VISIT_JAVA_DOT_COM="Java by Oracle"
385 MSG_VISIT_ADOPTOPENJDK="Java by AdoptOpenJDK"
390 MSG_ERROR_LAUNCHING="FEHLER beim Starten von '${CFBundleName}'."
391 MSG_MISSING_MAINCLASS="Die 'MainClass' ist nicht spezifiziert!\nDie Java-Anwendung kann nicht gestartet werden!"
392 MSG_JVMVERSION_REQ_INVALID="Die Syntax der angeforderten Java-Version ist ungültig: %s\nBitte kontaktieren Sie den Entwickler der App."
393 MSG_NO_SUITABLE_JAVA="Es wurde keine passende Java-Version auf Ihrem System gefunden!\nDieses Programm benötigt Java %s"
394 MSG_JAVA_VERSION_OR_LATER="oder neuer"
395 MSG_JAVA_VERSION_LATEST="(neuste Unterversion)"
396 MSG_JAVA_VERSION_MAX="bis %s"
397 MSG_NO_SUITABLE_JAVA_CHECK="Stellen Sie sicher, dass die angeforderte Java-Version installiert ist."
398 MSG_INSTALL_JAVA="Auf Ihrem System muss die 'Java'-Software installiert sein.\nBesuchen Sie java.com für weitere Installationshinweise."
400 MSG_VISIT_JAVA_DOT_COM="Java von Oracle"
401 MSG_VISIT_ADOPTOPENJDK="Java von AdoptOpenJDK"
406 MSG_ERROR_LAUNCHING="无法启动 '${CFBundleName}'."
407 MSG_MISSING_MAINCLASS="没有指定 'MainClass'!\nJava程序无法启动!"
408 MSG_JVMVERSION_REQ_INVALID="Java版本参数语法错误: %s\n请联系该应用的开发者。"
409 MSG_NO_SUITABLE_JAVA="没有在系统中找到合适的Java版本!\n必须安装Java %s才能够使用该程序!"
410 MSG_JAVA_VERSION_OR_LATER="及以上版本"
411 MSG_JAVA_VERSION_LATEST="(最新版本)"
412 MSG_JAVA_VERSION_MAX="最高为 %s"
413 MSG_NO_SUITABLE_JAVA_CHECK="请确保系统中安装了所需的Java版本"
414 MSG_INSTALL_JAVA="你需要在Mac中安装Java运行环境!\n访问 java.com 了解如何安装。"
416 MSG_VISIT_JAVA_DOT_COM="Java by Oracle"
417 MSG_VISIT_ADOPTOPENJDK="Java by AdoptOpenJDK"
422 MSG_ERROR_LAUNCHING="ERROR iniciando '${CFBundleName}'."
423 MSG_MISSING_MAINCLASS="¡'MainClass' no especificada!\n¡La aplicación Java no puede iniciarse!"
424 MSG_JVMVERSION_REQ_INVALID="La sintaxis de la versión Java requerida no es válida: %s\nPor favor, contacte con el desarrollador de la aplicación."
425 MSG_NO_SUITABLE_JAVA="¡No se encontró una versión de Java adecuada en su sistema!\nEste programa requiere Java %s"
426 MSG_JAVA_VERSION_OR_LATER="o posterior"
427 MSG_JAVA_VERSION_LATEST="(ultima actualización)"
428 MSG_JAVA_VERSION_MAX="superior a %s"
429 MSG_NO_SUITABLE_JAVA_CHECK="Asegúrese de instalar la versión Java requerida."
430 MSG_INSTALL_JAVA="¡Necesita tener JAVA instalado en su Mac!\nVisite java.com para consultar las instrucciones para su instalación..."
431 MSG_LATER="Más tarde"
432 MSG_VISIT_JAVA_DOT_COM="Java de Oracle"
433 MSG_VISIT_ADOPTOPENJDK="Java de AdoptOpenJDK"
438 MSG_ERROR_LAUNCHING="ERROR launching '${CFBundleName}'."
439 MSG_MISSING_MAINCLASS="'MainClass' isn't specified!\nJava application cannot be started!"
440 MSG_JVMVERSION_REQ_INVALID="The syntax of the required Java version is invalid: %s\nPlease contact the App developer."
441 MSG_NO_SUITABLE_JAVA="No suitable Java version found on your system!\nThis program requires Java %s"
442 MSG_JAVA_VERSION_OR_LATER="or later"
443 MSG_JAVA_VERSION_LATEST="(latest update)"
444 MSG_JAVA_VERSION_MAX="up to %s"
445 MSG_NO_SUITABLE_JAVA_CHECK="Make sure you install the required Java version."
446 MSG_INSTALL_JAVA="You need to have JAVA installed on your Mac!\nVisit java.com for installation instructions..."
448 MSG_VISIT_JAVA_DOT_COM="Java by Oracle"
449 MSG_VISIT_ADOPTOPENJDK="Java by AdoptOpenJDK"
455 # function 'get_java_version_from_cmd()'
457 # returns Java version string from 'java -version' command
458 # works for both old (1.8) and new (9) version schema
460 # @param1 path to a java JVM executable
461 # @return the Java version number as displayed in 'java -version' command
462 ################################################################################
463 function get_java_version_from_cmd() {
464 # second sed command strips " and -ea from the version string
465 echo $("$1" -version 2>&1 | awk '/version/{print $3}' | sed -E 's/"//g;s/-ea//g')
469 # function 'extract_java_major_version()'
471 # extract Java major version from a version string
473 # @param1 a Java version number ('1.8.0_45') or requirement string ('1.8+')
474 # @return the major version (e.g. '7', '8' or '9', etc.)
475 ################################################################################
476 function extract_java_major_version() {
477 echo $(echo "$1" | sed -E 's/^1\.//;s/^([0-9]+)(-ea|(\.[0-9_.]{1,7})?)(-b[0-9]+-[0-9]+)?[+*]?$/\1/')
481 # function 'get_comparable_java_version()'
483 # return comparable version for a Java version number or requirement string
485 # @param1 a Java version number ('1.8.0_45') or requirement string ('1.8+')
486 # @return an 8 digit numeral ('1.8.0_45'->'08000045'; '9.1.13'->'09001013')
487 ################################################################################
488 function get_comparable_java_version() {
489 # cleaning: 1) remove leading '1.'; 2) remove build string (e.g. '-b14-468'); 3) remove 'a-Z' and '-*+' (e.g. '-ea'); 4) replace '_' with '.'
490 local cleaned=$(echo "$1" | sed -E 's/^1\.//g;s/-b[0-9]+-[0-9]+$//g;s/[a-zA-Z+*\-]//g;s/_/./g')
491 # splitting at '.' into an array
492 local arr=( ${cleaned//./ } )
493 # echo a string with left padded version numbers
494 echo "$(printf '%02s' ${arr[0]})$(printf '%03s' ${arr[1]})$(printf '%03s' ${arr[2]})"
498 # function 'is_valid_requirement_pattern()'
500 # check whether the Java requirement is a valid requirement pattern
502 # supported requirements are for example:
503 # - 1.6 requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
504 # - 1.6* requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
505 # - 1.6+ requires Java 6 or higher [1.6, 1.6.0_45, 1.8, 9, etc.]
506 # - 1.6.0 requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
507 # - 1.6.0_45 requires Java 6u45 [1.6.0_45]
508 # - 1.6.0_45+ requires Java 6u45 or higher [1.6.0_45, 1.6.0_88, 1.8, etc.]
509 # - 9 requires Java 9 (any update) [9.0.*, 9.1, 9.3, etc.]
510 # - 9* requires Java 9 (any update) [9.0.*, 9.1, 9.3, etc.]
511 # - 9+ requires Java 9 or higher [9.0, 9.1, 10, etc.]
512 # - 9.1 requires Java 9.1 (any update) [9.1.*, 9.1.2, 9.1.13, etc.]
513 # - 9.1* requires Java 9.1 (any update) [9.1.*, 9.1.2, 9.1.13, etc.]
514 # - 9.1+ requires Java 9.1 or higher [9.1, 9.2, 10, etc.]
515 # - 9.1.3 requires Java 9.1.3 [9.1.3]
516 # - 9.1.3* requires Java 9.1.3 (any update) [9.1.3]
517 # - 9.1.3+ requires Java 9.1.3 or higher [9.1.3, 9.1.4, 9.2.*, 10, etc.]
518 # - 10-ea requires Java 10 (early access release)
520 # unsupported requirement patterns are for example:
521 # - 1.2, 1.3, 1.9 Java 2, 3 are not supported
522 # - 1.9 Java 9 introduced a new versioning scheme
523 # - 6u45 known versioning syntax, but unsupported
524 # - 9-ea*, 9-ea+ early access releases paired with */+
525 # - 9., 9.*, 9.+ version ending with a .
526 # - 9.1., 9.1.*, 9.1.+ version ending with a .
527 # - 9.3.5.6 4 part version number is unsupported
529 # @param1 a Java requirement string ('1.8+')
530 # @return boolean exit code: 0 (is valid), 1 (is not valid)
531 ################################################################################
532 function is_valid_requirement_pattern() {
534 java8pattern='1\.[4-8](\.[0-9]+)?(\.0_[0-9]+)?[*+]?'
535 java9pattern='(9|1[0-9])(-ea|[*+]|(\.[0-9]+){1,2}[*+]?)?'
536 # test matches either old Java versioning scheme (up to 1.8) or new scheme (starting with 9)
537 if [[ ${java_req} =~ ^(${java8pattern}|${java9pattern})$ ]]; then
546 # determine which JVM to use
547 ############################################
549 # default Apple JRE plugin path (< 1.6)
550 apple_jre_plugin="/Library/Java/Home/bin/java"
551 apple_jre_version=$(get_java_version_from_cmd "${apple_jre_plugin}")
552 # default Oracle JRE plugin path (>= 1.7)
553 oracle_jre_plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"
554 oracle_jre_version=$(get_java_version_from_cmd "${oracle_jre_plugin}")
557 # first check system variable "$JAVA_HOME" -> has precedence over any other System JVM
558 stub_logger '[JavaSearch] Checking for $JAVA_HOME ...'
559 if [ -n "$JAVA_HOME" ] ; then
560 stub_logger "[JavaSearch] ... found JAVA_HOME with value $JAVA_HOME"
562 # PR 26: Allow specifying "$JAVA_HOME" relative to "$AppPackageFolder"
563 # which allows for bundling a custom version of Java inside your app!
564 if [[ $JAVA_HOME == /* ]] ; then
565 # if "$JAVA_HOME" starts with a Slash it's an absolute path
566 JAVACMD="$JAVA_HOME/bin/java"
567 stub_logger "[JavaSearch] ... parsing JAVA_HOME as absolute path to the executable '$JAVACMD'"
569 # otherwise it's a relative path to "$AppPackageFolder"
570 JAVACMD="$AppPackageFolder/$JAVA_HOME/bin/java"
571 stub_logger "[JavaSearch] ... parsing JAVA_HOME as relative path inside the App bundle to the executable '$JAVACMD'"
573 JAVACMD_version=$(get_comparable_java_version $(get_java_version_from_cmd "${JAVACMD}"))
575 stub_logger "[JavaSearch] ... haven't found JAVA_HOME"
579 # check for any other or a specific Java version
580 # also if $JAVA_HOME exists but isn't executable
581 if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
583 # add a warning in the syslog if JAVA_HOME is not executable or not found (#100)
584 if [ -n "$JAVA_HOME" ] ; then
585 stub_logger "[JavaSearch] ... but no 'java' executable was found at the JAVA_HOME location!"
588 stub_logger "[JavaSearch] Searching for JavaVirtualMachines on the system ..."
593 # first check whether JVMVersion string is a valid requirement string
594 if [ ! -z "${JVMVersion}" ] && ! is_valid_requirement_pattern ${JVMVersion} ; then
595 MSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf "${MSG_JVMVERSION_REQ_INVALID}" "${JVMVersion}")
597 stub_logger "[EXIT 4] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"
598 # display error message with AppleScript
599 osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_JVMVERSION_REQ_INVALID_EXPANDED}\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1${DialogWithIcon}"
603 # then check whether JVMMaxVersion string is a valid requirement string
604 if [ ! -z "${JVMMaxVersion}" ] && ! is_valid_requirement_pattern ${JVMMaxVersion} ; then
605 MSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf "${MSG_JVMVERSION_REQ_INVALID}" "${JVMMaxVersion}")
607 stub_logger "[EXIT 5] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"
608 # display error message with AppleScript
609 osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_JVMVERSION_REQ_INVALID_EXPANDED}\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1${DialogWithIcon}"
615 # find installed JavaVirtualMachines (JDK + JRE)
618 # read JDK's from '/usr/libexec/java_home --xml' command with PlistBuddy and a custom Dict iterator
619 # idea: https://stackoverflow.com/a/14085460/1128689 and https://scriptingosx.com/2018/07/parsing-dscl-output-in-scripts/
620 javaXml=$(/usr/libexec/java_home --xml)
621 javaCounter=$(/usr/libexec/PlistBuddy -c "Print" /dev/stdin <<< $javaXml | grep "Dict" | wc -l | tr -d ' ')
623 # iterate over all Dict entries
624 # but only if there are any JVMs at all (#93)
625 if [ "$javaCounter" -gt "0" ] ; then
626 for idx in $(seq 0 $((javaCounter - 1)))
628 version=$(/usr/libexec/PlistBuddy -c "print :$idx:JVMVersion" /dev/stdin <<< $javaXml)
629 path=$(/usr/libexec/PlistBuddy -c "print :$idx:JVMHomePath" /dev/stdin <<< $javaXml)
631 allJVMs+=("$version:$path")
633 # unset for loop variables
637 # add SDKMAN! java versions (#95)
638 if [ -d ~/.sdkman/candidates/java/ ] ; then
639 for sdkjdk in ~/.sdkman/candidates/java/*/
641 if [[ ${sdkjdk} =~ /current/$ ]] ; then
645 sdkjdkcmd="${sdkjdk}bin/java"
646 version=$(get_java_version_from_cmd "${sdkjdkcmd}")
647 allJVMs+=("$version:$sdkjdkcmd")
649 # unset for loop variables
653 # add Apple JRE if available
654 if [ -x "${apple_jre_plugin}" ] ; then
655 allJVMs+=("$apple_jre_version:$apple_jre_plugin")
658 # add Oracle JRE if available
659 if [ -x "${oracle_jre_plugin}" ] ; then
660 allJVMs+=("$oracle_jre_version:$oracle_jre_plugin")
664 for i in "${allJVMs[@]}"
666 stub_logger "[JavaSearch] ... found JVM: $i"
670 # determine JVMs matching the min/max version requirement
672 stub_logger "[JavaSearch] Filtering the result list for JVMs matching the min/max version requirement ..."
674 minC=$(get_comparable_java_version ${JVMVersion})
675 maxC=$(get_comparable_java_version ${JVMMaxVersion})
678 for i in "${allJVMs[@]}"
680 # split JVM string at ':' delimiter to retain spaces in $path substring
681 IFS=: arr=($i) ; unset IFS
682 # [0] JVM version number
684 # comparable JVM version number
685 comp=$(get_comparable_java_version $ver)
688 # construct string item for adding to the "matchingJVMs" array
689 item="$comp:$ver:$path"
691 # pre-requisite: current version number needs to be greater than min version number
692 if [ "$comp" -ge "$minC" ] ; then
694 # perform max version checks if max version requirement is present
695 if [ ! -z ${JVMMaxVersion} ] ; then
697 # max version requirement ends with '*' modifier
698 if [[ ${JVMMaxVersion} == *\* ]] ; then
700 # use the '*' modifier from the max version string as wildcard for a 'starts with' comparison
701 # and check whether the current version number starts with the max version wildcard string
702 if [[ ${ver} == ${JVMMaxVersion} ]]; then
703 matchingJVMs+=("$item")
705 # or whether the current comparable version is lower than the comparable max version
706 elif [ "$comp" -le "$maxC" ] ; then
707 matchingJVMs+=("$item")
710 # max version requirement ends with '+' modifier -> always add this version if it's greater than $min
711 # because a max requirement with + modifier doesn't make sense
712 elif [[ ${JVMMaxVersion} == *+ ]] ; then
713 matchingJVMs+=("$item")
715 # matches 6 zeros at the end of the max version string (e.g. for 1.8, 9)
716 # -> then the max version string should be treated like with a '*' modifier at the end
717 #elif [[ ${maxC} =~ ^[0-9]{2}0{6}$ ]] && [ "$comp" -le $(( ${maxC#0} + 999 )) ] ; then
718 # matchingJVMs+=("$item")
720 # matches 3 zeros at the end of the max version string (e.g. for 9.1, 10.3)
721 # -> then the max version string should be treated like with a '*' modifier at the end
722 #elif [[ ${maxC} =~ ^[0-9]{5}0{3}$ ]] && [ "$comp" -le "${maxC}" ] ; then
723 # matchingJVMs+=("$item")
725 # matches standard requirements without modifier
726 elif [ "$comp" -le "$maxC" ]; then
727 matchingJVMs+=("$item")
730 # no max version requirement:
732 # min version requirement ends with '+' modifier
733 # -> always add the current version because it's greater than $min
734 elif [[ ${JVMVersion} == *+ ]] ; then
735 matchingJVMs+=("$item")
737 # min version requirement ends with '*' modifier
738 # -> use the '*' modifier from the min version string as wildcard for a 'starts with' comparison
739 # and check whether the current version number starts with the min version wildcard string
740 elif [[ ${JVMVersion} == *\* ]] ; then
741 if [[ ${ver} == ${JVMVersion} ]] ; then
742 matchingJVMs+=("$item")
745 # compare the min version against the current version with an additional * wildcard for a 'starts with' comparison
746 # -> e.g. add 1.8.0_44 when the requirement is 1.8
747 elif [[ ${ver} == ${JVMVersion}* ]] ; then
748 matchingJVMs+=("$item")
752 # unset for loop variables
753 unset arr ver comp path item
756 for i in "${matchingJVMs[@]}"
758 stub_logger "[JavaSearch] ... matches all requirements: $i"
762 # sort the matching JavaVirtualMachines by version number
763 # https://stackoverflow.com/a/11789688/1128689
764 IFS=$'\n' matchingJVMs=($(sort -nr <<<"${matchingJVMs[*]}"))
768 # get the highest matching JVM
769 for ((i = 0; i < ${#matchingJVMs[@]}; i++));
771 # split JVM string at ':' delimiter to retain spaces in $path substring
772 IFS=: arr=(${matchingJVMs[$i]}) ; unset IFS
773 # [0] comparable JVM version number
775 # [1] JVM version number
780 # use current value as JAVACMD if it's executable
781 if [ -x "$path" ] ; then
783 JAVACMD_version=$comp
787 # unset for loop variables
788 unset arr comp ver path
791 # log the Java Command and the extracted version number
792 stub_logger "[JavaCommand] '$JAVACMD'"
793 stub_logger "[JavaVersion] $(get_java_version_from_cmd "${JAVACMD}")${JAVACMD_version:+ / $JAVACMD_version}"
797 if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
799 # different error messages when a specific JVM was required
800 if [ ! -z "${JVMVersion}" ] ; then
801 # display human readable java version (#28)
802 java_version_hr=$(echo ${JVMVersion} | sed -E 's/^1\.([0-9+*]+)$/ \1/g' | sed "s/+/ ${MSG_JAVA_VERSION_OR_LATER}/;s/*/ ${MSG_JAVA_VERSION_LATEST}/")
803 MSG_NO_SUITABLE_JAVA_EXPANDED=$(printf "${MSG_NO_SUITABLE_JAVA}" "${java_version_hr}").
805 if [ ! -z "${JVMMaxVersion}" ] ; then
806 java_version_hr=$(extract_java_major_version ${JVMVersion})
807 java_version_max_hr=$(echo ${JVMMaxVersion} | sed -E 's/^1\.([0-9+*]+)$/ \1/g' | sed "s/+//;s/*/ ${MSG_JAVA_VERSION_LATEST}/")
808 MSG_NO_SUITABLE_JAVA_EXPANDED="$(printf "${MSG_NO_SUITABLE_JAVA}" "${java_version_hr}") $(printf "${MSG_JAVA_VERSION_MAX}" "${java_version_max_hr}")"
812 stub_logger "[EXIT 3] ${MSG_NO_SUITABLE_JAVA_EXPANDED}"
814 # display error message with AppleScript
815 osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_NO_SUITABLE_JAVA_EXPANDED}\n${MSG_NO_SUITABLE_JAVA_CHECK}\" with title \"${CFBundleName}\" buttons {\" OK \", \"${MSG_VISIT_JAVA_DOT_COM}\", \"${MSG_VISIT_ADOPTOPENJDK}\"} default button 1${DialogWithIcon}" \
816 -e "set response to button returned of the result" \
817 -e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" then open location \"https://www.java.com/download/\"" \
818 -e "if response is \"${MSG_VISIT_ADOPTOPENJDK}\" then open location \"https://adoptopenjdk.net/releases.html\""
824 stub_logger "[EXIT 1] ${MSG_ERROR_LAUNCHING}"
825 # display error message with AppleScript
826 osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_INSTALL_JAVA}\" with title \"${CFBundleName}\" buttons {\"${MSG_LATER}\", \"${MSG_VISIT_JAVA_DOT_COM}\", \"${MSG_VISIT_ADOPTOPENJDK}\"} default button 1${DialogWithIcon}" \
827 -e "set response to button returned of the result" \
828 -e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" then open location \"https://www.java.com/download/\"" \
829 -e "if response is \"${MSG_VISIT_ADOPTOPENJDK}\" then open location \"https://adoptopenjdk.net/releases.html\""
838 ############################################
840 if [ -z "${JVMMainClass}" ]; then
842 stub_logger "[EXIT 2] ${MSG_MISSING_MAINCLASS}"
843 # display error message with AppleScript
844 osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_MISSING_MAINCLASS}\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1${DialogWithIcon}"
851 # execute $JAVACMD and do some preparation
852 ############################################
854 # enable drag&drop to the dock icon
855 export CFProcessPath="$0"
857 # remove Apples ProcessSerialNumber from passthru arguments (#39)
858 if [[ "$*" == -psn* ]] ; then
864 # change to Working Directory based upon Apple/Oracle Plist info
865 cd "${WorkingDirectory}" || exit 13
866 stub_logger "[WorkingDirectory] ${WorkingDirectory}"
868 # execute Java and set
873 # - JVM options / properties (-D)
874 # - JVM default options (-X)
876 # - main class arguments
877 # - passthrough arguments from Terminal or Drag'n'Drop to Finder icon
878 stub_logger "[Exec] \"$JAVACMD\" -cp \"${JVMClassPath}\" ${JVMSplashFile:+ -splash:\"${ResourcesFolder}/${JVMSplashFile}\"} -Xdock:icon=\"${ResourcesFolder}/${CFBundleIconFile}\" -Xdock:name=\"${CFBundleName}\" ${JVMOptionsArr:+$(printf "'%s' " "${JVMOptionsArr[@]}") }${JVMDefaultOptions:+$JVMDefaultOptions }${JVMMainClass}${MainArgsArr:+ $(printf "'%s' " "${MainArgsArr[@]}")}${ArgsPassthru:+ $(printf "'%s' " "${ArgsPassthru[@]}")}"
880 -cp "${JVMClassPath}" \
881 ${JVMSplashFile:+ -splash:"${ResourcesFolder}/${JVMSplashFile}"} \
882 -Xdock:icon="${ResourcesFolder}/${CFBundleIconFile}" \
883 -Xdock:name="${CFBundleName}" \
884 ${JVMOptionsArr:+"${JVMOptionsArr[@]}" }\
885 ${JVMDefaultOptions:+$JVMDefaultOptions }\
887 ${MainArgsArr:+ "${MainArgsArr[@]}"}\
888 ${ArgsPassthru:+ "${ArgsPassthru[@]}"}