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 ##################################################################################
44 # Fix fonts. I don't know why the getting the
45 # basename of the app set to . matters, but it does
50 ./`basename "$0"` "$@"
54 export FREETYPE_PROPERTIES=truetype:interpreter-version=35
56 # function 'stub_logger()'
58 # A logger which logs to the macOS Console.app using the 'syslog' command
60 # @param1 the log message
62 ################################################################################
63 function stub_logger() {
65 Facility com.apple.console \
67 Sender "$(basename "$0")" \
68 Message "[$$][${CFBundleName:-$(basename "$0")}] $1"
73 # set the directory abspath of the current
74 # shell script with symlinks being resolved
75 ############################################
78 while [ -h "$PRG" ]; do
80 link=$(expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null)
81 if expr "$link" : '^/' 2> /dev/null >/dev/null; then
84 PRG="$(dirname "$PRG")/$link"
87 PROGDIR=$(dirname "$PRG")
88 stub_logger "[StubDir] $PROGDIR"
92 # set files and folders
93 ############################################
95 # the absolute path of the app package
96 cd "$PROGDIR"/../../ || exit 11
97 AppPackageFolder=$(pwd)
99 # the base path of the app package
101 AppPackageRoot=$(pwd)
103 # set Apple's Java folder
104 AppleJavaFolder="${AppPackageFolder}"/Contents/Resources/Java
106 # set Apple's Resources folder
107 AppleResourcesFolder="${AppPackageFolder}"/Contents/Resources
109 # set Oracle's Java folder
110 OracleJavaFolder="${AppPackageFolder}"/Contents/Java
112 # set Oracle's Resources folder
113 OracleResourcesFolder="${AppPackageFolder}"/Contents/Resources
115 # set path to Info.plist in bundle
116 InfoPlistFile="${AppPackageFolder}"/Contents/Info.plist
118 # set the default JVM Version to a null string
124 # function 'plist_get()'
126 # read a specific Plist key with 'PlistBuddy' utility
128 # @param1 the Plist key with leading colon ':'
129 # @return the value as String or Array
130 ################################################################################
132 /usr/libexec/PlistBuddy -c "print $1" "${InfoPlistFile}" 2> /dev/null
135 # function 'plist_get_java()'
137 # read a specific Plist key with 'PlistBuddy' utility
138 # in the 'Java' or 'JavaX' dictionary (<dict>)
140 # @param1 the Plist :Java(X):Key with leading colon ':'
141 # @return the value as String or Array
142 ################################################################################
144 plist_get ${JavaKey:-":Java"}$1
149 # read Info.plist and extract JVM options
150 ############################################
152 # read the program name from CFBundleName
153 CFBundleName=$(plist_get ':CFBundleName')
155 # read the icon file name
156 CFBundleIconFile=$(plist_get ':CFBundleIconFile')
159 # check Info.plist for Apple style Java keys -> if key :Java is present, parse in apple mode
160 /usr/libexec/PlistBuddy -c "print :Java" "${InfoPlistFile}" > /dev/null 2>&1
164 # if no :Java key is present, check Info.plist for universalJavaApplication style JavaX keys -> if key :JavaX is present, parse in apple mode
165 if [ $exitcode -ne 0 ]; then
166 /usr/libexec/PlistBuddy -c "print :JavaX" "${InfoPlistFile}" > /dev/null 2>&1
172 # read 'Info.plist' file in Apple style if exit code returns 0 (true, ':Java' key is present)
173 if [ $exitcode -eq 0 ]; then
174 stub_logger "[PlistStyle] Apple"
176 # set Java and Resources folder
177 JavaFolder="${AppleJavaFolder}"
178 ResourcesFolder="${AppleResourcesFolder}"
180 # set expandable variables
181 APP_ROOT="${AppPackageFolder}"
182 APP_PACKAGE="${AppPackageFolder}"
183 JAVAROOT="${AppleJavaFolder}"
187 # read the Java WorkingDirectory
188 JVMWorkDir=$(plist_get_java ':WorkingDirectory' | xargs)
189 # set Working Directory based upon PList value
190 if [[ ! -z ${JVMWorkDir} ]]; then
191 WorkingDirectory="${JVMWorkDir}"
193 # AppPackageRoot is the standard WorkingDirectory when the script is started
194 WorkingDirectory="${AppPackageRoot}"
196 # expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
197 WorkingDirectory=$(eval echo "${WorkingDirectory}")
200 # read the MainClass name
201 JVMMainClass="$(plist_get_java ':MainClass')"
203 # read the SplashFile name
204 JVMSplashFile=$(plist_get_java ':SplashFile')
206 # read the JVM Properties as an array and retain spaces
208 JVMOptions=($(xargs -n1 <<<$(plist_get_java ':Properties' | grep " =" | sed 's/^ */-D/g' | sed -E 's/ = (.*)$/="\1"/g')))
210 # post processing of the array follows further below...
212 # read the ClassPath in either Array or String style
213 JVMClassPath_RAW=$(plist_get_java ':ClassPath' | xargs)
214 if [[ $JVMClassPath_RAW == *Array* ]] ; then
215 JVMClassPath=.$(plist_get_java ':ClassPath' | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs)
217 JVMClassPath=${JVMClassPath_RAW}
219 # expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
220 JVMClassPath=$(eval echo "${JVMClassPath}")
222 # read the JVM Options in either Array or String style
223 JVMDefaultOptions_RAW=$(plist_get_java ':VMOptions' | xargs)
224 if [[ $JVMDefaultOptions_RAW == *Array* ]] ; then
225 JVMDefaultOptions=$(plist_get_java ':VMOptions' | grep " " | sed 's/^ */ /g' | tr -d '\n' | xargs)
227 JVMDefaultOptions=${JVMDefaultOptions_RAW}
229 # expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME (#84)
230 JVMDefaultOptions=$(eval echo "${JVMDefaultOptions}")
232 # read StartOnMainThread and add as -XstartOnFirstThread
233 JVMStartOnMainThread=$(plist_get_java ':StartOnMainThread')
234 if [ "${JVMStartOnMainThread}" == "true" ]; then
235 JVMDefaultOptions+=" -XstartOnFirstThread"
238 # read the JVM Arguments in either Array or String style (#76) and retain spaces
240 MainArgs_RAW=$(plist_get_java ':Arguments' | xargs)
241 if [[ $MainArgs_RAW == *Array* ]] ; then
242 MainArgs=($(xargs -n1 <<<$(plist_get_java ':Arguments' | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/ */ /g')))
244 MainArgs=($(xargs -n1 <<<$(plist_get_java ':Arguments')))
247 # post processing of the array follows further below...
249 # read the Java version we want to find
250 JVMVersion=$(plist_get_java ':JVMVersion' | xargs)
251 # post processing of the version string follows below...
254 # read 'Info.plist' file in Oracle style
256 stub_logger "[PlistStyle] Oracle"
258 # set Working Directory and Java and Resources folder
259 JavaFolder="${OracleJavaFolder}"
260 ResourcesFolder="${OracleResourcesFolder}"
261 WorkingDirectory="${OracleJavaFolder}"
263 # set expandable variables
264 APP_ROOT="${AppPackageFolder}"
265 APP_PACKAGE="${AppPackageFolder}"
266 JAVAROOT="${OracleJavaFolder}"
269 # read the MainClass name
270 JVMMainClass="$(plist_get ':JVMMainClassName')"
272 # read the SplashFile name
273 JVMSplashFile=$(plist_get ':JVMSplashFile')
275 # read the JVM Options as an array and retain spaces
277 JVMOptions=($(plist_get ':JVMOptions' | grep " " | sed 's/^ *//g'))
279 # post processing of the array follows further below...
281 # read the ClassPath in either Array or String style
282 JVMClassPath_RAW=$(plist_get ':JVMClassPath')
283 if [[ $JVMClassPath_RAW == *Array* ]] ; then
284 JVMClassPath=.$(plist_get ':JVMClassPath' | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs)
285 # expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
286 JVMClassPath=$(eval echo "${JVMClassPath}")
288 elif [[ ! -z ${JVMClassPath_RAW} ]] ; then
289 JVMClassPath=${JVMClassPath_RAW}
290 # expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
291 JVMClassPath=$(eval echo "${JVMClassPath}")
294 #default: fallback to OracleJavaFolder
295 JVMClassPath="${JavaFolder}/*"
296 # Do NOT expand the default 'AppName.app/Contents/Java/*' classpath (#42)
299 # read the JVM Default Options by parsing the :JVMDefaultOptions <dict>
300 # and pulling all <string> values starting with a dash (-)
301 JVMDefaultOptions=$(plist_get ':JVMDefaultOptions' | grep -o " \-.*" | tr -d '\n' | xargs)
302 # expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME (#99)
303 JVMDefaultOptions=$(eval echo "${JVMDefaultOptions}")
305 # read the Main Arguments from JVMArguments key as an array and retain spaces (see #46 for naming details)
307 MainArgs=($(xargs -n1 <<<$(plist_get ':JVMArguments' | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/ */ /g')))
309 # post processing of the array follows further below...
311 # read the Java version we want to find
312 JVMVersion=$(plist_get ':JVMVersion' | xargs)
313 # post processing of the version string follows below...
317 # (#75) check for undefined icons or icon names without .icns extension and prepare
318 # an osascript statement for those cases when the icon can be shown in the dialog
320 if [ ! -z ${CFBundleIconFile} ]; then
321 if [[ ${CFBundleIconFile} == *.icns ]] && [[ -f "${ResourcesFolder}/${CFBundleIconFile}" ]] ; then
322 DialogWithIcon=" with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
323 elif [[ ${CFBundleIconFile} != *.icns ]] && [[ -f "${ResourcesFolder}/${CFBundleIconFile}.icns" ]] ; then
324 CFBundleIconFile+=".icns"
325 DialogWithIcon=" with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
330 # JVMVersion: post processing and optional splitting
331 if [[ ${JVMVersion} == *";"* ]]; then
332 minMaxArray=(${JVMVersion//;/ })
333 JVMVersion=${minMaxArray[0]//+}
334 JVMMaxVersion=${minMaxArray[1]//+}
336 stub_logger "[JavaRequirement] JVM minimum version: ${JVMVersion}"
337 stub_logger "[JavaRequirement] JVM maximum version: ${JVMMaxVersion}"
339 # MainArgs: expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
341 for i in "${MainArgs[@]}"
343 MainArgsArr+=("$(eval echo "$i")")
346 # JVMOptions: expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
348 for i in "${JVMOptions[@]}"
350 JVMOptionsArr+=("$(eval echo "$i")")
354 # internationalized messages
355 ############################################
357 # supported languages / available translations
358 stubLanguages="^(fr|de|zh|es|en)-"
360 # read user preferred languages as defined in macOS System Preferences (#101)
361 stub_logger '[LanguageSearch] Checking preferred languages in macOS System Preferences...'
362 appleLanguages=($(defaults read -g AppleLanguages | grep '\s"' | tr -d ',' | xargs))
363 stub_logger "[LanguageSearch] ... found [${appleLanguages[*]}]"
366 for i in "${appleLanguages[@]}"
369 if [[ "$i" =~ $stubLanguages ]]; then
370 stub_logger "[LanguageSearch] ... selected '$i' ('$langValue') as the default language for the launcher stub"
371 language=${langValue}
375 if [ -z "${language}" ]; then
377 stub_logger "[LanguageSearch] ... selected fallback 'en' as the default language for the launcher stub"
379 stub_logger "[Language] $language"
382 case "${language}" in
385 MSG_ERROR_LAUNCHING="ERREUR au lancement de '${CFBundleName}'."
386 MSG_MISSING_MAINCLASS="'MainClass' n'est pas spécifié.\nL'application Java ne peut pas être lancée."
387 MSG_JVMVERSION_REQ_INVALID="La syntaxe de la version de Java demandée est invalide: %s\nVeuillez contacter le développeur de l'application."
388 MSG_NO_SUITABLE_JAVA="La version de Java installée sur votre système ne convient pas.\nCe programme nécessite Java %s"
389 MSG_JAVA_VERSION_OR_LATER="ou ultérieur"
390 MSG_JAVA_VERSION_LATEST="(dernière mise à jour)"
391 MSG_JAVA_VERSION_MAX="à %s"
392 MSG_NO_SUITABLE_JAVA_CHECK="Merci de bien vouloir installer la version de Java requise."
393 MSG_INSTALL_JAVA="Java doit être installé sur votre système.\nRendez-vous sur java.com et suivez les instructions d'installation..."
394 MSG_LATER="Plus tard"
395 MSG_VISIT_JAVA_DOT_COM="Java by Oracle"
396 MSG_VISIT_ADOPTIUM="Java by Adoptium"
401 MSG_ERROR_LAUNCHING="FEHLER beim Starten von '${CFBundleName}'."
402 MSG_MISSING_MAINCLASS="Die 'MainClass' ist nicht spezifiziert!\nDie Java-Anwendung kann nicht gestartet werden!"
403 MSG_JVMVERSION_REQ_INVALID="Die Syntax der angeforderten Java-Version ist ungültig: %s\nBitte kontaktieren Sie den Entwickler der App."
404 MSG_NO_SUITABLE_JAVA="Es wurde keine passende Java-Version auf Ihrem System gefunden!\nDieses Programm benötigt Java %s"
405 MSG_JAVA_VERSION_OR_LATER="oder neuer"
406 MSG_JAVA_VERSION_LATEST="(neuste Unterversion)"
407 MSG_JAVA_VERSION_MAX="bis %s"
408 MSG_NO_SUITABLE_JAVA_CHECK="Stellen Sie sicher, dass die angeforderte Java-Version installiert ist."
409 MSG_INSTALL_JAVA="Auf Ihrem System muss die 'Java'-Software installiert sein.\nBesuchen Sie java.com für weitere Installationshinweise."
411 MSG_VISIT_JAVA_DOT_COM="Java von Oracle"
412 MSG_VISIT_ADOPTIUM="Java von Adoptium"
417 MSG_ERROR_LAUNCHING="无法启动 '${CFBundleName}'."
418 MSG_MISSING_MAINCLASS="没有指定 'MainClass'!\nJava程序无法启动!"
419 MSG_JVMVERSION_REQ_INVALID="Java版本参数语法错误: %s\n请联系该应用的开发者。"
420 MSG_NO_SUITABLE_JAVA="没有在系统中找到合适的Java版本!\n必须安装Java %s才能够使用该程序!"
421 MSG_JAVA_VERSION_OR_LATER="及以上版本"
422 MSG_JAVA_VERSION_LATEST="(最新版本)"
423 MSG_JAVA_VERSION_MAX="最高为 %s"
424 MSG_NO_SUITABLE_JAVA_CHECK="请确保系统中安装了所需的Java版本"
425 MSG_INSTALL_JAVA="你需要在Mac中安装Java运行环境!\n访问 java.com 了解如何安装。"
427 MSG_VISIT_JAVA_DOT_COM="Java by Oracle"
428 MSG_VISIT_ADOPTIUM="Java by Adoptium"
433 MSG_ERROR_LAUNCHING="ERROR iniciando '${CFBundleName}'."
434 MSG_MISSING_MAINCLASS="¡'MainClass' no especificada!\n¡La aplicación Java no puede iniciarse!"
435 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."
436 MSG_NO_SUITABLE_JAVA="¡No se encontró una versión de Java adecuada en su sistema!\nEste programa requiere Java %s"
437 MSG_JAVA_VERSION_OR_LATER="o posterior"
438 MSG_JAVA_VERSION_LATEST="(ultima actualización)"
439 MSG_JAVA_VERSION_MAX="superior a %s"
440 MSG_NO_SUITABLE_JAVA_CHECK="Asegúrese de instalar la versión Java requerida."
441 MSG_INSTALL_JAVA="¡Necesita tener JAVA instalado en su Mac!\nVisite java.com para consultar las instrucciones para su instalación..."
442 MSG_LATER="Más tarde"
443 MSG_VISIT_JAVA_DOT_COM="Java de Oracle"
444 MSG_VISIT_ADOPTIUM="Java de Adoptium"
449 MSG_ERROR_LAUNCHING="ERROR launching '${CFBundleName}'."
450 MSG_MISSING_MAINCLASS="'MainClass' isn't specified!\nJava application cannot be started!"
451 MSG_JVMVERSION_REQ_INVALID="The syntax of the required Java version is invalid: %s\nPlease contact the App developer."
452 MSG_NO_SUITABLE_JAVA="No suitable Java version found on your system!\nThis program requires Java %s"
453 MSG_JAVA_VERSION_OR_LATER="or later"
454 MSG_JAVA_VERSION_LATEST="(latest update)"
455 MSG_JAVA_VERSION_MAX="up to %s"
456 MSG_NO_SUITABLE_JAVA_CHECK="Make sure you install the required Java version."
457 MSG_INSTALL_JAVA="You need to have JAVA installed on your Mac!\nVisit java.com for installation instructions..."
459 MSG_VISIT_JAVA_DOT_COM="Java by Oracle"
460 MSG_VISIT_ADOPTIUM="Java by Adoptium"
466 # function 'get_java_version_from_cmd()'
468 # returns Java version string from 'java -version' command
469 # works for both old (1.8) and new (9) version schema
471 # @param1 path to a java JVM executable
472 # @return the Java version number as displayed in 'java -version' command
473 ################################################################################
474 function get_java_version_from_cmd() {
475 # second sed command strips " and -ea from the version string
476 echo $("$1" -version 2>&1 | awk '/version/{print $3}' | sed -E 's/"//g;s/-ea//g')
480 # function 'extract_java_major_version()'
482 # extract Java major version from a version string
484 # @param1 a Java version number ('1.8.0_45') or requirement string ('1.8+')
485 # @return the major version (e.g. '7', '8' or '9', etc.)
486 ################################################################################
487 function extract_java_major_version() {
488 echo $(echo "$1" | sed -E 's/^1\.//;s/^([0-9]+)(-ea|(\.[0-9_.]{1,7})?)(-b[0-9]+-[0-9]+)?[+*]?$/\1/')
492 # function 'get_comparable_java_version()'
494 # return comparable version for a Java version number or requirement string
496 # @param1 a Java version number ('1.8.0_45') or requirement string ('1.8+')
497 # @return an 8 digit numeral ('1.8.0_45'->'08000045'; '9.1.13'->'09001013')
498 ################################################################################
499 function get_comparable_java_version() {
500 # cleaning: 1) remove leading '1.'; 2) remove build string (e.g. '-b14-468'); 3) remove 'a-Z' and '-*+' (e.g. '-ea'); 4) replace '_' with '.'
501 local cleaned=$(echo "$1" | sed -E 's/^1\.//g;s/-b[0-9]+-[0-9]+$//g;s/[a-zA-Z+*\-]//g;s/_/./g')
502 # splitting at '.' into an array
503 local arr=( ${cleaned//./ } )
504 # echo a string with left padded version numbers
505 echo "$(printf '%02s' ${arr[0]})$(printf '%03s' ${arr[1]})$(printf '%03s' ${arr[2]})"
509 # function 'is_valid_requirement_pattern()'
511 # check whether the Java requirement is a valid requirement pattern
513 # supported requirements are for example:
514 # - 1.6 requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
515 # - 1.6* requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
516 # - 1.6+ requires Java 6 or higher [1.6, 1.6.0_45, 1.8, 9, etc.]
517 # - 1.6.0 requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
518 # - 1.6.0_45 requires Java 6u45 [1.6.0_45]
519 # - 1.6.0_45+ requires Java 6u45 or higher [1.6.0_45, 1.6.0_88, 1.8, etc.]
520 # - 9 requires Java 9 (any update) [9.0.*, 9.1, 9.3, etc.]
521 # - 9* requires Java 9 (any update) [9.0.*, 9.1, 9.3, etc.]
522 # - 9+ requires Java 9 or higher [9.0, 9.1, 10, etc.]
523 # - 9.1 requires Java 9.1 (any update) [9.1.*, 9.1.2, 9.1.13, etc.]
524 # - 9.1* requires Java 9.1 (any update) [9.1.*, 9.1.2, 9.1.13, etc.]
525 # - 9.1+ requires Java 9.1 or higher [9.1, 9.2, 10, etc.]
526 # - 9.1.3 requires Java 9.1.3 [9.1.3]
527 # - 9.1.3* requires Java 9.1.3 (any update) [9.1.3]
528 # - 9.1.3+ requires Java 9.1.3 or higher [9.1.3, 9.1.4, 9.2.*, 10, etc.]
529 # - 10-ea requires Java 10 (early access release)
531 # unsupported requirement patterns are for example:
532 # - 1.2, 1.3, 1.9 Java 2, 3 are not supported
533 # - 1.9 Java 9 introduced a new versioning scheme
534 # - 6u45 known versioning syntax, but unsupported
535 # - 9-ea*, 9-ea+ early access releases paired with */+
536 # - 9., 9.*, 9.+ version ending with a .
537 # - 9.1., 9.1.*, 9.1.+ version ending with a .
538 # - 9.3.5.6 4 part version number is unsupported
540 # @param1 a Java requirement string ('1.8+')
541 # @return boolean exit code: 0 (is valid), 1 (is not valid)
542 ################################################################################
543 function is_valid_requirement_pattern() {
545 java8pattern='1\.[4-8](\.[0-9]+)?(\.0_[0-9]+)?[*+]?'
546 java9pattern='(9|1[0-9])(-ea|[*+]|(\.[0-9]+){1,2}[*+]?)?'
547 # test matches either old Java versioning scheme (up to 1.8) or new scheme (starting with 9)
548 if [[ ${java_req} =~ ^(${java8pattern}|${java9pattern})$ ]]; then
557 # determine which JVM to use
558 ############################################
560 # default Apple JRE plugin path (< 1.6)
561 apple_jre_plugin="/Library/Java/Home/bin/java"
562 apple_jre_version=$(get_java_version_from_cmd "${apple_jre_plugin}")
563 # default Oracle JRE plugin path (>= 1.7)
564 oracle_jre_plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"
565 oracle_jre_version=$(get_java_version_from_cmd "${oracle_jre_plugin}")
568 # first check system variable "$JAVA_HOME" -> has precedence over any other System JVM
569 stub_logger '[JavaSearch] Checking for $JAVA_HOME ...'
570 if [ -n "$JAVA_HOME" ] ; then
571 stub_logger "[JavaSearch] ... found JAVA_HOME with value $JAVA_HOME"
573 # PR 26: Allow specifying "$JAVA_HOME" relative to "$AppPackageFolder"
574 # which allows for bundling a custom version of Java inside your app!
575 if [[ $JAVA_HOME == /* ]] ; then
576 # if "$JAVA_HOME" starts with a Slash it's an absolute path
577 JAVACMD="$JAVA_HOME/bin/java"
578 stub_logger "[JavaSearch] ... parsing JAVA_HOME as absolute path to the executable '$JAVACMD'"
580 # otherwise it's a relative path to "$AppPackageFolder"
581 JAVACMD="$AppPackageFolder/$JAVA_HOME/bin/java"
582 stub_logger "[JavaSearch] ... parsing JAVA_HOME as relative path inside the App bundle to the executable '$JAVACMD'"
584 JAVACMD_version=$(get_comparable_java_version $(get_java_version_from_cmd "${JAVACMD}"))
586 stub_logger "[JavaSearch] ... haven't found JAVA_HOME"
590 # check for any other or a specific Java version
591 # also if $JAVA_HOME exists but isn't executable
592 if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
594 # add a warning in the syslog if JAVA_HOME is not executable or not found (#100)
595 if [ -n "$JAVA_HOME" ] ; then
596 stub_logger "[JavaSearch] ... but no 'java' executable was found at the JAVA_HOME location!"
599 stub_logger "[JavaSearch] Searching for JavaVirtualMachines on the system ..."
604 # first check whether JVMVersion string is a valid requirement string
605 if [ ! -z "${JVMVersion}" ] && ! is_valid_requirement_pattern ${JVMVersion} ; then
606 MSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf "${MSG_JVMVERSION_REQ_INVALID}" "${JVMVersion}")
608 stub_logger "[EXIT 4] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"
609 # display error message with AppleScript
610 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}"
614 # then check whether JVMMaxVersion string is a valid requirement string
615 if [ ! -z "${JVMMaxVersion}" ] && ! is_valid_requirement_pattern ${JVMMaxVersion} ; then
616 MSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf "${MSG_JVMVERSION_REQ_INVALID}" "${JVMMaxVersion}")
618 stub_logger "[EXIT 5] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"
619 # display error message with AppleScript
620 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}"
626 # find installed JavaVirtualMachines (JDK + JRE)
629 # read JDK's from '/usr/libexec/java_home --xml' command with PlistBuddy and a custom Dict iterator
630 # idea: https://stackoverflow.com/a/14085460/1128689 and https://scriptingosx.com/2018/07/parsing-dscl-output-in-scripts/
631 javaXml=$(/usr/libexec/java_home --xml)
632 javaCounter=$(/usr/libexec/PlistBuddy -c "Print" /dev/stdin <<< $javaXml | grep "Dict" | wc -l | tr -d ' ')
634 # iterate over all Dict entries
635 # but only if there are any JVMs at all (#93)
636 if [ "$javaCounter" -gt "0" ] ; then
637 for idx in $(seq 0 $((javaCounter - 1)))
639 version=$(/usr/libexec/PlistBuddy -c "print :$idx:JVMVersion" /dev/stdin <<< $javaXml)
640 path=$(/usr/libexec/PlistBuddy -c "print :$idx:JVMHomePath" /dev/stdin <<< $javaXml)
642 allJVMs+=("$version:$path")
644 # unset for loop variables
648 # add SDKMAN! java versions (#95)
649 if [ -d ~/.sdkman/candidates/java/ ] ; then
650 for sdkjdk in ~/.sdkman/candidates/java/*/
652 if [[ ${sdkjdk} =~ /current/$ ]] ; then
656 sdkjdkcmd="${sdkjdk}bin/java"
657 version=$(get_java_version_from_cmd "${sdkjdkcmd}")
658 allJVMs+=("$version:$sdkjdkcmd")
660 # unset for loop variables
664 # add Apple JRE if available
665 if [ -x "${apple_jre_plugin}" ] ; then
666 allJVMs+=("$apple_jre_version:$apple_jre_plugin")
669 # add Oracle JRE if available
670 if [ -x "${oracle_jre_plugin}" ] ; then
671 allJVMs+=("$oracle_jre_version:$oracle_jre_plugin")
675 for i in "${allJVMs[@]}"
677 stub_logger "[JavaSearch] ... found JVM: $i"
681 # determine JVMs matching the min/max version requirement
683 stub_logger "[JavaSearch] Filtering the result list for JVMs matching the min/max version requirement ..."
685 minC=$(get_comparable_java_version ${JVMVersion})
686 maxC=$(get_comparable_java_version ${JVMMaxVersion})
689 for i in "${allJVMs[@]}"
691 # split JVM string at ':' delimiter to retain spaces in $path substring
692 IFS=: arr=($i) ; unset IFS
693 # [0] JVM version number
695 # comparable JVM version number
696 comp=$(get_comparable_java_version $ver)
699 # construct string item for adding to the "matchingJVMs" array
700 item="$comp:$ver:$path"
702 # pre-requisite: current version number needs to be greater than min version number
703 if [ "$comp" -ge "$minC" ] ; then
705 # perform max version checks if max version requirement is present
706 if [ ! -z ${JVMMaxVersion} ] ; then
708 # max version requirement ends with '*' modifier
709 if [[ ${JVMMaxVersion} == *\* ]] ; then
711 # use the '*' modifier from the max version string as wildcard for a 'starts with' comparison
712 # and check whether the current version number starts with the max version wildcard string
713 if [[ ${ver} == ${JVMMaxVersion} ]]; then
714 matchingJVMs+=("$item")
716 # or whether the current comparable version is lower than the comparable max version
717 elif [ "$comp" -le "$maxC" ] ; then
718 matchingJVMs+=("$item")
721 # max version requirement ends with '+' modifier -> always add this version if it's greater than $min
722 # because a max requirement with + modifier doesn't make sense
723 elif [[ ${JVMMaxVersion} == *+ ]] ; then
724 matchingJVMs+=("$item")
726 # matches 6 zeros at the end of the max version string (e.g. for 1.8, 9)
727 # -> then the max version string should be treated like with a '*' modifier at the end
728 #elif [[ ${maxC} =~ ^[0-9]{2}0{6}$ ]] && [ "$comp" -le $(( ${maxC#0} + 999 )) ] ; then
729 # matchingJVMs+=("$item")
731 # matches 3 zeros at the end of the max version string (e.g. for 9.1, 10.3)
732 # -> then the max version string should be treated like with a '*' modifier at the end
733 #elif [[ ${maxC} =~ ^[0-9]{5}0{3}$ ]] && [ "$comp" -le "${maxC}" ] ; then
734 # matchingJVMs+=("$item")
736 # matches standard requirements without modifier
737 elif [ "$comp" -le "$maxC" ]; then
738 matchingJVMs+=("$item")
741 # no max version requirement:
743 # min version requirement ends with '+' modifier
744 # -> always add the current version because it's greater than $min
745 elif [[ ${JVMVersion} == *+ ]] ; then
746 matchingJVMs+=("$item")
748 # min version requirement ends with '*' modifier
749 # -> use the '*' modifier from the min version string as wildcard for a 'starts with' comparison
750 # and check whether the current version number starts with the min version wildcard string
751 elif [[ ${JVMVersion} == *\* ]] ; then
752 if [[ ${ver} == ${JVMVersion} ]] ; then
753 matchingJVMs+=("$item")
756 # compare the min version against the current version with an additional * wildcard for a 'starts with' comparison
757 # -> e.g. add 1.8.0_44 when the requirement is 1.8
758 elif [[ ${ver} == ${JVMVersion}* ]] ; then
759 matchingJVMs+=("$item")
763 # unset for loop variables
764 unset arr ver comp path item
767 for i in "${matchingJVMs[@]}"
769 stub_logger "[JavaSearch] ... matches all requirements: $i"
773 # sort the matching JavaVirtualMachines by version number
774 # https://stackoverflow.com/a/11789688/1128689
775 IFS=$'\n' matchingJVMs=($(sort -nr <<<"${matchingJVMs[*]}"))
779 # get the highest matching JVM
780 for ((i = 0; i < ${#matchingJVMs[@]}; i++));
782 # split JVM string at ':' delimiter to retain spaces in $path substring
783 IFS=: arr=(${matchingJVMs[$i]}) ; unset IFS
784 # [0] comparable JVM version number
786 # [1] JVM version number
791 # use current value as JAVACMD if it's executable
792 if [ -x "$path" ] ; then
794 JAVACMD_version=$comp
798 # unset for loop variables
799 unset arr comp ver path
802 # log the Java Command and the extracted version number
803 stub_logger "[JavaCommand] '$JAVACMD'"
804 stub_logger "[JavaVersion] $(get_java_version_from_cmd "${JAVACMD}")${JAVACMD_version:+ / $JAVACMD_version}"
806 # Make sure tabbing mode is disabled for the selected java version
808 CFBundleIdentifier=net.java.openjdk.$(get_java_version_from_cmd "${JAVACMD}").java
810 if [ x$(defaults read ${CFBundleIdentifier} AppleWindowTabbingMode) != "xnever" ]; then
811 defaults write ${CFBundleIdentifier} AppleWindowTabbingMode never
814 if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
816 # different error messages when a specific JVM was required
817 if [ ! -z "${JVMVersion}" ] ; then
818 # display human readable java version (#28)
819 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}/")
820 MSG_NO_SUITABLE_JAVA_EXPANDED=$(printf "${MSG_NO_SUITABLE_JAVA}" "${java_version_hr}").
822 if [ ! -z "${JVMMaxVersion}" ] ; then
823 java_version_hr=$(extract_java_major_version ${JVMVersion})
824 java_version_max_hr=$(echo ${JVMMaxVersion} | sed -E 's/^1\.([0-9+*]+)$/ \1/g' | sed "s/+//;s/*/ ${MSG_JAVA_VERSION_LATEST}/")
825 MSG_NO_SUITABLE_JAVA_EXPANDED="$(printf "${MSG_NO_SUITABLE_JAVA}" "${java_version_hr}") $(printf "${MSG_JAVA_VERSION_MAX}" "${java_version_max_hr}")"
829 stub_logger "[EXIT 3] ${MSG_NO_SUITABLE_JAVA_EXPANDED}"
831 # display error message with AppleScript
832 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_ADOPTIUM}\"} default button 1${DialogWithIcon}" \
833 -e "set response to button returned of the result" \
834 -e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" then open location \"https://www.java.com/download/\"" \
835 -e "if response is \"${MSG_VISIT_ADOPTIUM}\" then open location \"https://adoptium.net/releases.html\""
841 stub_logger "[EXIT 1] ${MSG_ERROR_LAUNCHING}"
842 # display error message with AppleScript
843 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_ADOPTIUM}\"} default button 1${DialogWithIcon}" \
844 -e "set response to button returned of the result" \
845 -e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" then open location \"https://www.java.com/download/\"" \
846 -e "if response is \"${MSG_VISIT_ADOPTIUM}\" then open location \"https://adoptium.net/releases.html\""
855 ############################################
857 if [ -z "${JVMMainClass}" ]; then
859 stub_logger "[EXIT 2] ${MSG_MISSING_MAINCLASS}"
860 # display error message with AppleScript
861 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}"
868 # execute $JAVACMD and do some preparation
869 ############################################
871 # enable drag&drop to the dock icon
872 export CFProcessPath="$0"
874 # remove Apples ProcessSerialNumber from passthru arguments (#39)
875 if [[ "$*" == -psn* ]] ; then
881 # change to Working Directory based upon Apple/Oracle Plist info
882 cd "${WorkingDirectory}" || exit 13
883 stub_logger "[WorkingDirectory] ${WorkingDirectory}"
885 # execute Java and set
890 # - JVM options / properties (-D)
891 # - JVM default options (-X)
893 # - main class arguments
894 # - passthrough arguments from Terminal or Drag'n'Drop to Finder icon
895 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[@]}")}"
897 -Djava.library.path="${AppleJavaFolder}" \
898 -cp "${JVMClassPath}" \
899 ${JVMSplashFile:+ -splash:"${ResourcesFolder}/${JVMSplashFile}"} \
900 -Xdock:icon="${ResourcesFolder}/${CFBundleIconFile}" \
901 -Xdock:name="${CFBundleName}" \
902 ${JVMOptionsArr:+"${JVMOptionsArr[@]}" }\
903 ${JVMDefaultOptions:+$JVMDefaultOptions }\
905 ${MainArgsArr:+ "${MainArgsArr[@]}"}\
906 ${ArgsPassthru:+ "${ArgsPassthru[@]}"}