3 # Fix fonts. I don't know why the getting the
4 # basename of the app set to . matters, but it does
13 export FREETYPE_PROPERTIES=truetype:interpreter-version=35
14 ##################################################################################
16 # universalJavaApplicationStub #
18 # A BASH based JavaApplicationStub for Java Apps on Mac OS X #
19 # that works with both Apple's and Oracle's plist format. #
21 # Inspired by Ian Roberts stackoverflow answer #
22 # at http://stackoverflow.com/a/17546508/1128689 #
24 # @author Tobias Fischer #
25 # @url https://github.com/tofi86/universalJavaApplicationStub #
29 ##################################################################################
31 # The MIT License (MIT) #
33 # Copyright (c) 2014-2018 Tobias Fischer #
35 # Permission is hereby granted, free of charge, to any person obtaining a copy #
36 # of this software and associated documentation files (the "Software"), to deal #
37 # in the Software without restriction, including without limitation the rights #
38 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
39 # copies of the Software, and to permit persons to whom the Software is #
40 # furnished to do so, subject to the following conditions: #
42 # The above copyright notice and this permission notice shall be included in all #
43 # copies or substantial portions of the Software. #
45 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
46 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
47 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
48 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
49 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
50 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
53 ##################################################################################
57 # function 'stub_logger()'
59 # A logger which logs to the macOS Console.app using the 'syslog' command
61 # @param1 the log message
63 ################################################################################
64 function stub_logger() {
66 Facility com.apple.console \
68 Sender "$(basename "$0")" \
69 Message "[$$][${CFBundleName:-$(basename "$0")}] $1"
74 # set the directory abspath of the current
75 # shell script with symlinks being resolved
76 ############################################
79 while [ -h "$PRG" ]; do
81 link=$(expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null)
82 if expr "$link" : '^/' 2> /dev/null >/dev/null; then
85 PRG="$(dirname "$PRG")/$link"
88 PROGDIR=$(dirname "$PRG")
89 stub_logger "[StubDir] $PROGDIR"
93 # set files and folders
94 ############################################
96 # the absolute path of the app package
97 cd "$PROGDIR"/../../ || exit 11
98 AppPackageFolder=$(pwd)
100 # the base path of the app package
102 AppPackageRoot=$(pwd)
104 # set Apple's Java folder
105 AppleJavaFolder="${AppPackageFolder}"/Contents/Resources/Java
107 # set Apple's Resources folder
108 AppleResourcesFolder="${AppPackageFolder}"/Contents/Resources
110 # set Oracle's Java folder
111 OracleJavaFolder="${AppPackageFolder}"/Contents/Java
113 # set Oracle's Resources folder
114 OracleResourcesFolder="${AppPackageFolder}"/Contents/Resources
116 # set path to Info.plist in bundle
117 InfoPlistFile="${AppPackageFolder}"/Contents/Info.plist
119 # set the default JVM Version to a null string
125 # function 'plist_get()'
127 # read a specific Plist key with 'PlistBuddy' utility
129 # @param1 the Plist key with leading colon ':'
130 # @return the value as String or Array
131 ################################################################################
133 /usr/libexec/PlistBuddy -c "print $1" "${InfoPlistFile}" 2> /dev/null
136 # function 'plist_get_java()'
138 # read a specific Plist key with 'PlistBuddy' utility
139 # in the 'Java' or 'JavaX' dictionary (<dict>)
141 # @param1 the Plist :Java(X):Key with leading colon ':'
142 # @return the value as String or Array
143 ################################################################################
145 plist_get ${JavaKey:-":Java"}$1
150 # read Info.plist and extract JVM options
151 ############################################
153 # read the program name from CFBundleName
154 CFBundleName=$(plist_get ':CFBundleName')
156 # read the icon file name
157 CFBundleIconFile=$(plist_get ':CFBundleIconFile')
160 # check Info.plist for Apple style Java keys -> if key :Java is present, parse in apple mode
161 /usr/libexec/PlistBuddy -c "print :Java" "${InfoPlistFile}" > /dev/null 2>&1
165 # if no :Java key is present, check Info.plist for universalJavaApplication style JavaX keys -> if key :JavaX is present, parse in apple mode
166 if [ $exitcode -ne 0 ]; then
167 /usr/libexec/PlistBuddy -c "print :JavaX" "${InfoPlistFile}" > /dev/null 2>&1
173 # read 'Info.plist' file in Apple style if exit code returns 0 (true, ':Java' key is present)
174 if [ $exitcode -eq 0 ]; then
175 stub_logger "[PlistStyle] Apple"
177 # set Java and Resources folder
178 JavaFolder="${AppleJavaFolder}"
179 ResourcesFolder="${AppleResourcesFolder}"
181 APP_PACKAGE="${AppPackageFolder}"
182 JAVAROOT="${AppleJavaFolder}"
186 # read the Java WorkingDirectory
187 JVMWorkDir=$(plist_get_java ':WorkingDirectory' | xargs)
188 # set Working Directory based upon PList value
189 if [[ ! -z ${JVMWorkDir} ]]; then
190 WorkingDirectory="${JVMWorkDir}"
192 # AppPackageRoot is the standard WorkingDirectory when the script is started
193 WorkingDirectory="${AppPackageRoot}"
195 # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
196 WorkingDirectory=$(eval echo "${WorkingDirectory}")
199 # read the MainClass name
200 JVMMainClass="$(plist_get_java ':MainClass')"
202 # read the SplashFile name
203 JVMSplashFile=$(plist_get_java ':SplashFile')
205 # read the JVM Properties as an array and retain spaces
207 JVMOptions=($(xargs -n1 <<<$(plist_get_java ':Properties' | grep " =" | sed 's/^ */-D/g' | sed -E 's/ = (.*)$/="\1"/g')))
209 # post processing of the array follows further below...
211 # read the ClassPath in either Array or String style
212 JVMClassPath_RAW=$(plist_get_java ':ClassPath' | xargs)
213 if [[ $JVMClassPath_RAW == *Array* ]] ; then
214 JVMClassPath=.$(plist_get_java ':ClassPath' | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs)
216 JVMClassPath=${JVMClassPath_RAW}
218 # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
219 JVMClassPath=$(eval echo "${JVMClassPath}")
221 # read the JVM Options in either Array or String style
222 JVMDefaultOptions_RAW=$(plist_get_java ':VMOptions' | xargs)
223 if [[ $JVMDefaultOptions_RAW == *Array* ]] ; then
224 JVMDefaultOptions=$(plist_get_java ':VMOptions' | grep " " | sed 's/^ */ /g' | tr -d '\n' | xargs)
226 JVMDefaultOptions=${JVMDefaultOptions_RAW}
229 # read StartOnMainThread and add as -XstartOnFirstThread
230 JVMStartOnMainThread=$(plist_get_java ':StartOnMainThread')
231 if [ "${JVMStartOnMainThread}" == "true" ]; then
232 JVMDefaultOptions+=" -XstartOnFirstThread"
235 # read the JVM Arguments as an array and retain spaces
237 MainArgs=($(xargs -n1 <<<$(plist_get_java ':Arguments')))
239 # post processing of the array follows further below...
241 # read the Java version we want to find
242 JVMVersion=$(plist_get_java ':JVMVersion' | xargs)
243 # post processing of the version string follows below...
246 # read 'Info.plist' file in Oracle style
248 stub_logger "[PlistStyle] Oracle"
250 # set Working Directory and Java and Resources folder
251 JavaFolder="${OracleJavaFolder}"
252 ResourcesFolder="${OracleResourcesFolder}"
253 WorkingDirectory="${OracleJavaFolder}"
255 APP_ROOT="${AppPackageFolder}"
257 # read the MainClass name
258 JVMMainClass="$(plist_get ':JVMMainClassName')"
260 # read the SplashFile name
261 JVMSplashFile=$(plist_get ':JVMSplashFile')
263 # read the JVM Options as an array and retain spaces
265 JVMOptions=($(plist_get ':JVMOptions' | grep " " | sed 's/^ *//g'))
267 # post processing of the array follows further below...
269 # read the ClassPath in either Array or String style
270 JVMClassPath_RAW=$(plist_get ':JVMClassPath')
271 if [[ $JVMClassPath_RAW == *Array* ]] ; then
272 JVMClassPath=.$(plist_get ':JVMClassPath' | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs)
273 # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
274 JVMClassPath=$(eval echo "${JVMClassPath}")
276 elif [[ ! -z ${JVMClassPath_RAW} ]] ; then
277 JVMClassPath=${JVMClassPath_RAW}
278 # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
279 JVMClassPath=$(eval echo "${JVMClassPath}")
282 #default: fallback to OracleJavaFolder
283 JVMClassPath="${JavaFolder}/*"
284 # Do NOT expand the default 'AppName.app/Contents/Java/*' classpath (#42)
287 # read the JVM Default Options
288 JVMDefaultOptions=$(plist_get ':JVMDefaultOptions' | grep -o " \-.*" | tr -d '\n' | xargs)
290 # read the Main Arguments from JVMArguments key as an array and retain spaces (see #46 for naming details)
292 MainArgs=($(xargs -n1 <<<$(plist_get ':JVMArguments' | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/ */ /g')))
294 # post processing of the array follows further below...
296 # read the Java version we want to find
297 JVMVersion=$(plist_get ':JVMVersion' | xargs)
298 # post processing of the version string follows below...
303 # JVMVersion: post processing and optional splitting
304 if [[ ${JVMVersion} == *";"* ]]; then
305 minMaxArray=(${JVMVersion//;/ })
306 JVMVersion=${minMaxArray[0]//+}
307 JVMMaxVersion=${minMaxArray[1]//+}
309 stub_logger "[JavaRequirement] JVM minimum version: ${JVMVersion}"
310 stub_logger "[JavaRequirement] JVM maximum version: ${JVMMaxVersion}"
312 # MainArgs: replace occurences of $APP_ROOT with its content
314 for i in "${MainArgs[@]}"
316 MainArgsArr+=("$(eval echo "$i")")
319 # JVMOptions: replace occurences of $APP_ROOT with its content
321 for i in "${JVMOptions[@]}"
323 JVMOptionsArr+=("$(eval echo "$i")")
327 # internationalized messages
328 ############################################
330 LANG=$(defaults read -g AppleLocale)
331 stub_logger "[Language] $LANG"
333 # French localization
334 if [[ $LANG == fr* ]] ; then
335 MSG_ERROR_LAUNCHING="ERREUR au lancement de '${CFBundleName}'."
336 MSG_MISSING_MAINCLASS="'MainClass' n'est pas spécifié.\nL'application Java ne peut pas être lancée."
337 MSG_JVMVERSION_REQ_INVALID="La syntaxe de la version Java demandée est invalide: %s\nVeuillez contacter le développeur de l'application."
338 MSG_NO_SUITABLE_JAVA="La version de Java installée sur votre système ne convient pas.\nCe programme nécessite Java %s"
339 MSG_JAVA_VERSION_OR_LATER="ou ultérieur"
340 MSG_JAVA_VERSION_LATEST="(dernière mise à jour)"
341 MSG_JAVA_VERSION_MAX="à %s"
342 MSG_NO_SUITABLE_JAVA_CHECK="Merci de bien vouloir installer la version de Java requise."
343 MSG_INSTALL_JAVA="Java doit être installé sur votre système.\nRendez-vous sur java.com et suivez les instructions d'installation..."
344 MSG_LATER="Plus tard"
345 MSG_VISIT_JAVA_DOT_COM="Visiter java.com"
347 # German localization
348 elif [[ $LANG == de* ]] ; then
349 MSG_ERROR_LAUNCHING="FEHLER beim Starten von '${CFBundleName}'."
350 MSG_MISSING_MAINCLASS="Die 'MainClass' ist nicht spezifiziert!\nDie Java-Anwendung kann nicht gestartet werden!"
351 MSG_JVMVERSION_REQ_INVALID="Die Syntax der angeforderten Java-Version ist ungültig: %s\nBitte kontaktieren Sie den Entwickler der App."
352 MSG_NO_SUITABLE_JAVA="Es wurde keine passende Java-Version auf Ihrem System gefunden!\nDieses Programm benötigt Java %s"
353 MSG_JAVA_VERSION_OR_LATER="oder neuer"
354 MSG_JAVA_VERSION_LATEST="(neuste Unterversion)"
355 MSG_JAVA_VERSION_MAX="bis %s"
356 MSG_NO_SUITABLE_JAVA_CHECK="Stellen Sie sicher, dass die angeforderte Java-Version installiert ist."
357 MSG_INSTALL_JAVA="Auf Ihrem System muss die 'Java'-Software installiert sein.\nBesuchen Sie java.com für weitere Installationshinweise."
359 MSG_VISIT_JAVA_DOT_COM="java.com öffnen"
361 # Simplifyed Chinese localization
362 elif [[ $LANG == zh* ]] ; then
363 MSG_ERROR_LAUNCHING="无法启动 '${CFBundleName}'."
364 MSG_MISSING_MAINCLASS="没有指定 'MainClass'!\nJava程序无法启动!"
365 MSG_JVMVERSION_REQ_INVALID="Java版本参数语法错误: %s\n请联系该应用的开发者。"
366 MSG_NO_SUITABLE_JAVA="没有在系统中找到合适的Java版本!\n必须安装Java %s才能够使用该程序!"
367 MSG_JAVA_VERSION_OR_LATER="及以上版本"
368 MSG_JAVA_VERSION_LATEST="(最新版本)"
369 MSG_JAVA_VERSION_MAX="最高为 %s"
370 MSG_NO_SUITABLE_JAVA_CHECK="请确保系统中安装了所需的Java版本"
371 MSG_INSTALL_JAVA="你需要在Mac中安装Java运行环境!\n访问 java.com 了解如何安装。"
373 MSG_VISIT_JAVA_DOT_COM="访问 java.com"
375 # English default localization
377 MSG_ERROR_LAUNCHING="ERROR launching '${CFBundleName}'."
378 MSG_MISSING_MAINCLASS="'MainClass' isn't specified!\nJava application cannot be started!"
379 MSG_JVMVERSION_REQ_INVALID="The syntax of the required Java version is invalid: %s\nPlease contact the App developer."
380 MSG_NO_SUITABLE_JAVA="No suitable Java version found on your system!\nThis program requires Java %s"
381 MSG_JAVA_VERSION_OR_LATER="or later"
382 MSG_JAVA_VERSION_LATEST="(latest update)"
383 MSG_JAVA_VERSION_MAX="up to %s"
384 MSG_NO_SUITABLE_JAVA_CHECK="Make sure you install the required Java version."
385 MSG_INSTALL_JAVA="You need to have JAVA installed on your Mac!\nVisit java.com for installation instructions..."
387 MSG_VISIT_JAVA_DOT_COM="Visit java.com"
392 # function 'get_java_version_from_cmd()'
394 # returns Java version string from 'java -version' command
395 # works for both old (1.8) and new (9) version schema
397 # @param1 path to a java JVM executable
398 # @return the Java version number as displayed in 'java -version' command
399 ################################################################################
400 function get_java_version_from_cmd() {
401 # second sed command strips " and -ea from the version string
402 echo $("$1" -version 2>&1 | awk '/version/{print $3}' | sed -E 's/"//g;s/-ea//g')
406 # function 'extract_java_major_version()'
408 # extract Java major version from a version string
410 # @param1 a Java version number ('1.8.0_45') or requirement string ('1.8+')
411 # @return the major version (e.g. '7', '8' or '9', etc.)
412 ################################################################################
413 function extract_java_major_version() {
414 echo $(echo "$1" | sed -E 's/^1\.//;s/^([0-9]+)(-ea|(\.[0-9_.]{1,7})?)(-b[0-9]+-[0-9]+)?[+*]?$/\1/')
418 # function 'get_comparable_java_version()'
420 # return comparable version for a Java version number or requirement string
422 # @param1 a Java version number ('1.8.0_45') or requirement string ('1.8+')
423 # @return an 8 digit numeral ('1.8.0_45'->'08000045'; '9.1.13'->'09001013')
424 ################################################################################
425 function get_comparable_java_version() {
426 # cleaning: 1) remove leading '1.'; 2) remove build string (e.g. '-b14-468'); 3) remove 'a-Z' and '-*+' (e.g. '-ea'); 4) replace '_' with '.'
427 local cleaned=$(echo "$1" | sed -E 's/^1\.//g;s/-b[0-9]+-[0-9]+$//g;s/[a-zA-Z+*\-]//g;s/_/./g')
428 # splitting at '.' into an array
429 local arr=( ${cleaned//./ } )
430 # echo a string with left padded version numbers
431 echo "$(printf '%02s' ${arr[0]})$(printf '%03s' ${arr[1]})$(printf '%03s' ${arr[2]})"
435 # function 'is_valid_requirement_pattern()'
437 # check whether the Java requirement is a valid requirement pattern
439 # supported requirements are for example:
440 # - 1.6 requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
441 # - 1.6* requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
442 # - 1.6+ requires Java 6 or higher [1.6, 1.6.0_45, 1.8, 9, etc.]
443 # - 1.6.0 requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
444 # - 1.6.0_45 requires Java 6u45 [1.6.0_45]
445 # - 1.6.0_45+ requires Java 6u45 or higher [1.6.0_45, 1.6.0_88, 1.8, etc.]
446 # - 9 requires Java 9 (any update) [9.0.*, 9.1, 9.3, etc.]
447 # - 9* requires Java 9 (any update) [9.0.*, 9.1, 9.3, etc.]
448 # - 9+ requires Java 9 or higher [9.0, 9.1, 10, etc.]
449 # - 9.1 requires Java 9.1 (any update) [9.1.*, 9.1.2, 9.1.13, etc.]
450 # - 9.1* requires Java 9.1 (any update) [9.1.*, 9.1.2, 9.1.13, etc.]
451 # - 9.1+ requires Java 9.1 or higher [9.1, 9.2, 10, etc.]
452 # - 9.1.3 requires Java 9.1.3 [9.1.3]
453 # - 9.1.3* requires Java 9.1.3 (any update) [9.1.3]
454 # - 9.1.3+ requires Java 9.1.3 or higher [9.1.3, 9.1.4, 9.2.*, 10, etc.]
455 # - 10-ea requires Java 10 (early access release)
457 # unsupported requirement patterns are for example:
458 # - 1.2, 1.3, 1.9 Java 2, 3 are not supported
459 # - 1.9 Java 9 introduced a new versioning scheme
460 # - 6u45 known versioning syntax, but unsupported
461 # - 9-ea*, 9-ea+ early access releases paired with */+
462 # - 9., 9.*, 9.+ version ending with a .
463 # - 9.1., 9.1.*, 9.1.+ version ending with a .
464 # - 9.3.5.6 4 part version number is unsupported
466 # @param1 a Java requirement string ('1.8+')
467 # @return boolean exit code: 0 (is valid), 1 (is not valid)
468 ################################################################################
469 function is_valid_requirement_pattern() {
471 java8pattern='1\.[4-8](\.0)?(\.0_[0-9]+)?[*+]?'
472 java9pattern='(9|1[0-9])(-ea|[*+]|(\.[0-9]+){1,2}[*+]?)?'
473 # test matches either old Java versioning scheme (up to 1.8) or new scheme (starting with 9)
474 if [[ ${java_req} =~ ^(${java8pattern}|${java9pattern})$ ]]; then
483 # determine which JVM to use
484 ############################################
486 # default Apple JRE plugin path (< 1.6)
487 apple_jre_plugin="/Library/Java/Home/bin/java"
488 apple_jre_version=$(get_java_version_from_cmd "${apple_jre_plugin}")
489 # default Oracle JRE plugin path (>= 1.7)
490 oracle_jre_plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"
491 oracle_jre_version=$(get_java_version_from_cmd "${oracle_jre_plugin}")
494 # first check system variable "$JAVA_HOME" -> has precedence over any other System JVM
495 stub_logger '[JavaSearch] Checking for $JAVA_HOME ...'
496 if [ -n "$JAVA_HOME" ] ; then
497 stub_logger "[JavaSearch] ... found JAVA_HOME with value $JAVA_HOME"
499 # PR 26: Allow specifying "$JAVA_HOME" relative to "$AppPackageFolder"
500 # which allows for bundling a custom version of Java inside your app!
501 if [[ $JAVA_HOME == /* ]] ; then
502 # if "$JAVA_HOME" starts with a Slash it's an absolute path
503 JAVACMD="$JAVA_HOME/bin/java"
505 # otherwise it's a relative path to "$AppPackageFolder"
506 JAVACMD="$AppPackageFolder/$JAVA_HOME/bin/java"
508 JAVACMD_version=$(get_comparable_java_version $(get_java_version_from_cmd "${JAVACMD}"))
510 stub_logger "[JavaSearch] ... didn't found JAVA_HOME"
514 # check for any other or a specific Java version
515 # also if $JAVA_HOME exists but isn't executable
516 if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
517 stub_logger "[JavaSearch] Checking for JavaVirtualMachines on the system ..."
522 # first check whether JVMVersion string is a valid requirement string
523 if [ ! -z "${JVMVersion}" ] && ! is_valid_requirement_pattern ${JVMVersion} ; then
524 MSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf "${MSG_JVMVERSION_REQ_INVALID}" "${JVMVersion}")
526 stub_logger "[EXIT 4] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"
527 # display error message with AppleScript
528 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 with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
532 # then check whether JVMMaxVersion string is a valid requirement string
533 if [ ! -z "${JVMMaxVersion}" ] && ! is_valid_requirement_pattern ${JVMMaxVersion} ; then
534 MSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf "${MSG_JVMVERSION_REQ_INVALID}" "${JVMMaxVersion}")
536 stub_logger "[EXIT 5] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"
537 # display error message with AppleScript
538 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 with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
544 # find installed JavaVirtualMachines (JDK + JRE)
546 # read JDK's from '/usr/libexec/java_home -V' command
547 while read -r line; do
548 version=$(echo $line | awk -F $',' '{print $1;}')
549 path=$(echo $line | awk -F $'" ' '{print $2;}')
551 allJVMs+=("$version:$path")
552 done < <(/usr/libexec/java_home -V 2>&1 | grep '^[[:space:]]')
553 # unset while loop variables
556 # add Apple JRE if available
557 if [ -x "${apple_jre_plugin}" ] ; then
558 allJVMs+=("$apple_jre_version:$apple_jre_plugin")
561 # add Oracle JRE if available
562 if [ -x "${oracle_jre_plugin}" ] ; then
563 allJVMs+=("$oracle_jre_version:$oracle_jre_plugin")
567 for i in "${allJVMs[@]}"
569 stub_logger "[JavaSearch] ... found JVM: $i"
573 # determine JVMs matching the min/max version requirement
574 minC=$(get_comparable_java_version ${JVMVersion})
575 maxC=$(get_comparable_java_version ${JVMMaxVersion})
578 for i in "${allJVMs[@]}"
580 # split JVM string at ':' delimiter to retain spaces in $path substring
581 IFS=: arr=($i) ; unset IFS
582 # [0] JVM version number
584 # comparable JVM version number
585 comp=$(get_comparable_java_version $ver)
588 # construct string item for adding to the "matchingJVMs" array
589 item="$comp:$ver:$path"
591 # pre-requisite: current version number needs to be greater than min version number
592 if [ "$comp" -ge "$minC" ] ; then
594 # perform max version checks if max version requirement is present
595 if [ ! -z ${JVMMaxVersion} ] ; then
597 # max version requirement ends with '*' modifier
598 if [[ ${JVMMaxVersion} == *\* ]] ; then
600 # use the '*' modifier from the max version string as wildcard for a 'starts with' comparison
601 # and check whether the current version number starts with the max version wildcard string
602 if [[ ${ver} == ${JVMMaxVersion} ]]; then
603 matchingJVMs+=("$item")
605 # or whether the current comparable version is lower than the comparable max version
606 elif [ "$comp" -le "$maxC" ] ; then
607 matchingJVMs+=("$item")
610 # max version requirement ends with '+' modifier -> always add this version if it's greater than $min
611 # because a max requirement with + modifier doesn't make sense
612 elif [[ ${JVMMaxVersion} == *+ ]] ; then
613 matchingJVMs+=("$item")
615 # matches 6 zeros at the end of the max version string (e.g. for 1.8, 9)
616 # -> then the max version string should be treated like with a '*' modifier at the end
617 #elif [[ ${maxC} =~ ^[0-9]{2}0{6}$ ]] && [ "$comp" -le $(( ${maxC#0} + 999 )) ] ; then
618 # matchingJVMs+=("$item")
620 # matches 3 zeros at the end of the max version string (e.g. for 9.1, 10.3)
621 # -> then the max version string should be treated like with a '*' modifier at the end
622 #elif [[ ${maxC} =~ ^[0-9]{5}0{3}$ ]] && [ "$comp" -le "${maxC}" ] ; then
623 # matchingJVMs+=("$item")
625 # matches standard requirements without modifier
626 elif [ "$comp" -le "$maxC" ]; then
627 matchingJVMs+=("$item")
630 # no max version requirement:
632 # min version requirement ends with '+' modifier
633 # -> always add the current version because it's greater than $min
634 elif [[ ${JVMVersion} == *+ ]] ; then
635 matchingJVMs+=("$item")
637 # min version requirement ends with '*' modifier
638 # -> use the '*' modifier from the min version string as wildcard for a 'starts with' comparison
639 # and check whether the current version number starts with the min version wildcard string
640 elif [[ ${JVMVersion} == *\* ]] ; then
641 if [[ ${ver} == ${JVMVersion} ]] ; then
642 matchingJVMs+=("$item")
645 # compare the min version against the current version with an additional * wildcard for a 'starts with' comparison
646 # -> e.g. add 1.8.0_44 when the requirement is 1.8
647 elif [[ ${ver} == ${JVMVersion}* ]] ; then
648 matchingJVMs+=("$item")
652 # unset for loop variables
653 unset arr ver comp path item
656 for i in "${matchingJVMs[@]}"
658 stub_logger "[JavaSearch] ... ... matches all requirements: $i"
662 # sort the matching JavaVirtualMachines by version number
663 # https://stackoverflow.com/a/11789688/1128689
664 IFS=$'\n' matchingJVMs=($(sort -nr <<<"${matchingJVMs[*]}"))
668 # get the highest matching JVM
669 for ((i = 0; i < ${#matchingJVMs[@]}; i++));
671 # split JVM string at ':' delimiter to retain spaces in $path substring
672 IFS=: arr=(${matchingJVMs[$i]}) ; unset IFS
673 # [0] comparable JVM version number
675 # [1] JVM version number
680 # use current value as JAVACMD if it's executable
681 if [ -x "$path" ] ; then
683 JAVACMD_version=$comp
687 # unset for loop variables
688 unset arr comp ver path
691 # log the Java Command and the extracted version number
692 stub_logger "[JavaCommand] '$JAVACMD'"
693 stub_logger "[JavaVersion] $(get_java_version_from_cmd "${JAVACMD}")${JAVACMD_version:+ / $JAVACMD_version}"
697 if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
699 # different error messages when a specific JVM was required
700 if [ ! -z "${JVMVersion}" ] ; then
701 # display human readable java version (#28)
702 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}/")
703 MSG_NO_SUITABLE_JAVA_EXPANDED=$(printf "${MSG_NO_SUITABLE_JAVA}" "${java_version_hr}").
705 if [ ! -z "${JVMMaxVersion}" ] ; then
706 java_version_hr=$(extract_java_major_version ${JVMVersion})
707 java_version_max_hr=$(echo ${JVMMaxVersion} | sed -E 's/^1\.([0-9+*]+)$/ \1/g' | sed "s/+//;s/*/ ${MSG_JAVA_VERSION_LATEST}/")
708 MSG_NO_SUITABLE_JAVA_EXPANDED="$(printf "${MSG_NO_SUITABLE_JAVA}" "${java_version_hr}") $(printf "${MSG_JAVA_VERSION_MAX}" "${java_version_max_hr}")"
712 stub_logger "[EXIT 3] ${MSG_NO_SUITABLE_JAVA_EXPANDED}"
714 # display error message with AppleScript
715 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}\"} default button \"${MSG_VISIT_JAVA_DOT_COM}\" with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)" \
716 -e "set response to button returned of the result" \
717 -e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" then open location \"http://java.com\""
723 stub_logger "[EXIT 1] ${MSG_ERROR_LAUNCHING}"
724 # display error message with AppleScript
725 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}\"} default button \"${MSG_VISIT_JAVA_DOT_COM}\" with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)" \
726 -e "set response to button returned of the result" \
727 -e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" then open location \"http://java.com\""
736 ############################################
738 if [ -z "${JVMMainClass}" ]; then
740 stub_logger "[EXIT 2] ${MSG_MISSING_MAINCLASS}"
741 # display error message with AppleScript
742 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 with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
749 # execute $JAVACMD and do some preparation
750 ############################################
752 # enable drag&drop to the dock icon
753 export CFProcessPath="$0"
755 # remove Apples ProcessSerialNumber from passthru arguments (#39)
756 if [[ "$*" == -psn* ]] ; then
762 # change to Working Directory based upon Apple/Oracle Plist info
763 cd "${WorkingDirectory}" || exit 13
764 stub_logger "[WorkingDirectory] ${WorkingDirectory}"
766 # execute Java and set
771 # - JVM options / properties (-D)
772 # - JVM default options (-X)
774 # - main class arguments
775 # - passthrough arguments from Terminal or Drag'n'Drop to Finder icon
776 stub_logger "[Exec] \"$JAVACMD\" -cp \"${JVMClassPath}\" -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[@]}")}"
778 -Djava.library.path="${AppleJavaFolder}" \
779 -cp "${JVMClassPath}" \
780 -splash:"${ResourcesFolder}/${JVMSplashFile}" \
781 -Xdock:icon="${ResourcesFolder}/${CFBundleIconFile}" \
782 -Xdock:name="${CFBundleName}" \
783 ${JVMOptionsArr:+"${JVMOptionsArr[@]}" }\
784 ${JVMDefaultOptions:+$JVMDefaultOptions }\
786 ${MainArgsArr:+ "${MainArgsArr[@]}"}\
787 ${ArgsPassthru:+ "${ArgsPassthru[@]}"}