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-2018 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 APP_PACKAGE="${AppPackageFolder}"
170 JAVAROOT="${AppleJavaFolder}"
174 # read the Java WorkingDirectory
175 JVMWorkDir=$(plist_get_java ':WorkingDirectory' | xargs)
176 # set Working Directory based upon PList value
177 if [[ ! -z ${JVMWorkDir} ]]; then
178 WorkingDirectory="${JVMWorkDir}"
180 # AppPackageRoot is the standard WorkingDirectory when the script is started
181 WorkingDirectory="${AppPackageRoot}"
183 # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
184 WorkingDirectory=$(eval echo "${WorkingDirectory}")
187 # read the MainClass name
188 JVMMainClass="$(plist_get_java ':MainClass')"
190 # read the SplashFile name
191 JVMSplashFile=$(plist_get_java ':SplashFile')
193 # read the JVM Properties as an array and retain spaces
195 JVMOptions=($(xargs -n1 <<<$(plist_get_java ':Properties' | grep " =" | sed 's/^ */-D/g' | sed -E 's/ = (.*)$/="\1"/g')))
197 # post processing of the array follows further below...
199 # read the ClassPath in either Array or String style
200 JVMClassPath_RAW=$(plist_get_java ':ClassPath' | xargs)
201 if [[ $JVMClassPath_RAW == *Array* ]] ; then
202 JVMClassPath=.$(plist_get_java ':ClassPath' | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs)
204 JVMClassPath=${JVMClassPath_RAW}
206 # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
207 JVMClassPath=$(eval echo "${JVMClassPath}")
209 # read the JVM Options in either Array or String style
210 JVMDefaultOptions_RAW=$(plist_get_java ':VMOptions' | xargs)
211 if [[ $JVMDefaultOptions_RAW == *Array* ]] ; then
212 JVMDefaultOptions=$(plist_get_java ':VMOptions' | grep " " | sed 's/^ */ /g' | tr -d '\n' | xargs)
214 JVMDefaultOptions=${JVMDefaultOptions_RAW}
217 # read StartOnMainThread and add as -XstartOnFirstThread
218 JVMStartOnMainThread=$(plist_get_java ':StartOnMainThread')
219 if [ "${JVMStartOnMainThread}" == "true" ]; then
220 JVMDefaultOptions+=" -XstartOnFirstThread"
223 # read the JVM Arguments as an array and retain spaces
225 MainArgs=($(xargs -n1 <<<$(plist_get_java ':Arguments')))
227 # post processing of the array follows further below...
229 # read the Java version we want to find
230 JVMVersion=$(plist_get_java ':JVMVersion' | xargs)
231 # post processing of the version string follows below...
234 # read 'Info.plist' file in Oracle style
236 stub_logger "[PlistStyle] Oracle"
238 # set Working Directory and Java and Resources folder
239 JavaFolder="${OracleJavaFolder}"
240 ResourcesFolder="${OracleResourcesFolder}"
241 WorkingDirectory="${OracleJavaFolder}"
243 APP_ROOT="${AppPackageFolder}"
245 # read the MainClass name
246 JVMMainClass="$(plist_get ':JVMMainClassName')"
248 # read the SplashFile name
249 JVMSplashFile=$(plist_get ':JVMSplashFile')
251 # read the JVM Options as an array and retain spaces
253 JVMOptions=($(plist_get ':JVMOptions' | grep " " | sed 's/^ *//g'))
255 # post processing of the array follows further below...
257 # read the ClassPath in either Array or String style
258 JVMClassPath_RAW=$(plist_get ':JVMClassPath')
259 if [[ $JVMClassPath_RAW == *Array* ]] ; then
260 JVMClassPath=.$(plist_get ':JVMClassPath' | grep " " | sed 's/^ */:/g' | tr -d '\n' | xargs)
261 # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
262 JVMClassPath=$(eval echo "${JVMClassPath}")
264 elif [[ ! -z ${JVMClassPath_RAW} ]] ; then
265 JVMClassPath=${JVMClassPath_RAW}
266 # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
267 JVMClassPath=$(eval echo "${JVMClassPath}")
270 #default: fallback to OracleJavaFolder
271 JVMClassPath="${JavaFolder}/*"
272 # Do NOT expand the default 'AppName.app/Contents/Java/*' classpath (#42)
275 # read the JVM Default Options
276 JVMDefaultOptions=$(plist_get ':JVMDefaultOptions' | grep -o " \-.*" | tr -d '\n' | xargs)
278 # read the Main Arguments from JVMArguments key as an array and retain spaces (see #46 for naming details)
280 MainArgs=($(xargs -n1 <<<$(plist_get ':JVMArguments' | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/ */ /g')))
282 # post processing of the array follows further below...
284 # read the Java version we want to find
285 JVMVersion=$(plist_get ':JVMVersion' | xargs)
286 # post processing of the version string follows below...
291 # JVMVersion: post processing and optional splitting
292 if [[ ${JVMVersion} == *";"* ]]; then
293 minMaxArray=(${JVMVersion//;/ })
294 JVMVersion=${minMaxArray[0]//+}
295 JVMMaxVersion=${minMaxArray[1]//+}
297 stub_logger "[JavaRequirement] JVM minimum version: ${JVMVersion}"
298 stub_logger "[JavaRequirement] JVM maximum version: ${JVMMaxVersion}"
300 # MainArgs: replace occurences of $APP_ROOT with its content
302 for i in "${MainArgs[@]}"
304 MainArgsArr+=("$(eval echo "$i")")
307 # JVMOptions: replace occurences of $APP_ROOT with its content
309 for i in "${JVMOptions[@]}"
311 JVMOptionsArr+=("$(eval echo "$i")")
315 # internationalized messages
316 ############################################
318 LANG=$(defaults read -g AppleLocale)
319 stub_logger "[Language] $LANG"
321 # French localization
322 if [[ $LANG == fr* ]] ; then
323 MSG_ERROR_LAUNCHING="ERREUR au lancement de '${CFBundleName}'."
324 MSG_MISSING_MAINCLASS="'MainClass' n'est pas spécifié.\nL'application Java ne peut pas être lancée."
325 MSG_JVMVERSION_REQ_INVALID="La syntaxe de la version Java demandée est invalide: %s\nVeuillez contacter le développeur de l'application."
326 MSG_NO_SUITABLE_JAVA="La version de Java installée sur votre système ne convient pas.\nCe programme nécessite Java %s"
327 MSG_JAVA_VERSION_OR_LATER="ou ultérieur"
328 MSG_JAVA_VERSION_LATEST="(dernière mise à jour)"
329 MSG_JAVA_VERSION_MAX="à %s"
330 MSG_NO_SUITABLE_JAVA_CHECK="Merci de bien vouloir installer la version de Java requise."
331 MSG_INSTALL_JAVA="Java doit être installé sur votre système.\nRendez-vous sur java.com et suivez les instructions d'installation..."
332 MSG_LATER="Plus tard"
333 MSG_VISIT_JAVA_DOT_COM="Visiter java.com"
335 # German localization
336 elif [[ $LANG == de* ]] ; then
337 MSG_ERROR_LAUNCHING="FEHLER beim Starten von '${CFBundleName}'."
338 MSG_MISSING_MAINCLASS="Die 'MainClass' ist nicht spezifiziert!\nDie Java-Anwendung kann nicht gestartet werden!"
339 MSG_JVMVERSION_REQ_INVALID="Die Syntax der angeforderten Java-Version ist ungültig: %s\nBitte kontaktieren Sie den Entwickler der App."
340 MSG_NO_SUITABLE_JAVA="Es wurde keine passende Java-Version auf Ihrem System gefunden!\nDieses Programm benötigt Java %s"
341 MSG_JAVA_VERSION_OR_LATER="oder neuer"
342 MSG_JAVA_VERSION_LATEST="(neuste Unterversion)"
343 MSG_JAVA_VERSION_MAX="bis %s"
344 MSG_NO_SUITABLE_JAVA_CHECK="Stellen Sie sicher, dass die angeforderte Java-Version installiert ist."
345 MSG_INSTALL_JAVA="Auf Ihrem System muss die 'Java'-Software installiert sein.\nBesuchen Sie java.com für weitere Installationshinweise."
347 MSG_VISIT_JAVA_DOT_COM="java.com öffnen"
349 # Simplifyed Chinese localization
350 elif [[ $LANG == zh* ]] ; then
351 MSG_ERROR_LAUNCHING="无法启动 '${CFBundleName}'."
352 MSG_MISSING_MAINCLASS="没有指定 'MainClass'!\nJava程序无法启动!"
353 MSG_JVMVERSION_REQ_INVALID="Java版本参数语法错误: %s\n请联系该应用的开发者。"
354 MSG_NO_SUITABLE_JAVA="没有在系统中找到合适的Java版本!\n必须安装Java %s才能够使用该程序!"
355 MSG_JAVA_VERSION_OR_LATER="及以上版本"
356 MSG_JAVA_VERSION_LATEST="(最新版本)"
357 MSG_JAVA_VERSION_MAX="最高为 %s"
358 MSG_NO_SUITABLE_JAVA_CHECK="请确保系统中安装了所需的Java版本"
359 MSG_INSTALL_JAVA="你需要在Mac中安装Java运行环境!\n访问 java.com 了解如何安装。"
361 MSG_VISIT_JAVA_DOT_COM="访问 java.com"
363 # English default localization
365 MSG_ERROR_LAUNCHING="ERROR launching '${CFBundleName}'."
366 MSG_MISSING_MAINCLASS="'MainClass' isn't specified!\nJava application cannot be started!"
367 MSG_JVMVERSION_REQ_INVALID="The syntax of the required Java version is invalid: %s\nPlease contact the App developer."
368 MSG_NO_SUITABLE_JAVA="No suitable Java version found on your system!\nThis program requires Java %s"
369 MSG_JAVA_VERSION_OR_LATER="or later"
370 MSG_JAVA_VERSION_LATEST="(latest update)"
371 MSG_JAVA_VERSION_MAX="up to %s"
372 MSG_NO_SUITABLE_JAVA_CHECK="Make sure you install the required Java version."
373 MSG_INSTALL_JAVA="You need to have JAVA installed on your Mac!\nVisit java.com for installation instructions..."
375 MSG_VISIT_JAVA_DOT_COM="Visit java.com"
380 # function 'get_java_version_from_cmd()'
382 # returns Java version string from 'java -version' command
383 # works for both old (1.8) and new (9) version schema
385 # @param1 path to a java JVM executable
386 # @return the Java version number as displayed in 'java -version' command
387 ################################################################################
388 function get_java_version_from_cmd() {
389 # second sed command strips " and -ea from the version string
390 echo $("$1" -version 2>&1 | awk '/version/{print $3}' | sed -E 's/"//g;s/-ea//g')
394 # function 'extract_java_major_version()'
396 # extract Java major version from a version string
398 # @param1 a Java version number ('1.8.0_45') or requirement string ('1.8+')
399 # @return the major version (e.g. '7', '8' or '9', etc.)
400 ################################################################################
401 function extract_java_major_version() {
402 echo $(echo "$1" | sed -E 's/^1\.//;s/^([0-9]+)(-ea|(\.[0-9_.]{1,7})?)(-b[0-9]+-[0-9]+)?[+*]?$/\1/')
406 # function 'get_comparable_java_version()'
408 # return comparable version for a Java version number or requirement string
410 # @param1 a Java version number ('1.8.0_45') or requirement string ('1.8+')
411 # @return an 8 digit numeral ('1.8.0_45'->'08000045'; '9.1.13'->'09001013')
412 ################################################################################
413 function get_comparable_java_version() {
414 # cleaning: 1) remove leading '1.'; 2) remove build string (e.g. '-b14-468'); 3) remove 'a-Z' and '-*+' (e.g. '-ea'); 4) replace '_' with '.'
415 local cleaned=$(echo "$1" | sed -E 's/^1\.//g;s/-b[0-9]+-[0-9]+$//g;s/[a-zA-Z+*\-]//g;s/_/./g')
416 # splitting at '.' into an array
417 local arr=( ${cleaned//./ } )
418 # echo a string with left padded version numbers
419 echo "$(printf '%02s' ${arr[0]})$(printf '%03s' ${arr[1]})$(printf '%03s' ${arr[2]})"
423 # function 'is_valid_requirement_pattern()'
425 # check whether the Java requirement is a valid requirement pattern
427 # supported requirements are for example:
428 # - 1.6 requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
429 # - 1.6* requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
430 # - 1.6+ requires Java 6 or higher [1.6, 1.6.0_45, 1.8, 9, etc.]
431 # - 1.6.0 requires Java 6 (any update) [1.6, 1.6.0_45, 1.6.0_88]
432 # - 1.6.0_45 requires Java 6u45 [1.6.0_45]
433 # - 1.6.0_45+ requires Java 6u45 or higher [1.6.0_45, 1.6.0_88, 1.8, etc.]
434 # - 9 requires Java 9 (any update) [9.0.*, 9.1, 9.3, etc.]
435 # - 9* requires Java 9 (any update) [9.0.*, 9.1, 9.3, etc.]
436 # - 9+ requires Java 9 or higher [9.0, 9.1, 10, etc.]
437 # - 9.1 requires Java 9.1 (any update) [9.1.*, 9.1.2, 9.1.13, etc.]
438 # - 9.1* requires Java 9.1 (any update) [9.1.*, 9.1.2, 9.1.13, etc.]
439 # - 9.1+ requires Java 9.1 or higher [9.1, 9.2, 10, etc.]
440 # - 9.1.3 requires Java 9.1.3 [9.1.3]
441 # - 9.1.3* requires Java 9.1.3 (any update) [9.1.3]
442 # - 9.1.3+ requires Java 9.1.3 or higher [9.1.3, 9.1.4, 9.2.*, 10, etc.]
443 # - 10-ea requires Java 10 (early access release)
445 # unsupported requirement patterns are for example:
446 # - 1.2, 1.3, 1.9 Java 2, 3 are not supported
447 # - 1.9 Java 9 introduced a new versioning scheme
448 # - 6u45 known versioning syntax, but unsupported
449 # - 9-ea*, 9-ea+ early access releases paired with */+
450 # - 9., 9.*, 9.+ version ending with a .
451 # - 9.1., 9.1.*, 9.1.+ version ending with a .
452 # - 9.3.5.6 4 part version number is unsupported
454 # @param1 a Java requirement string ('1.8+')
455 # @return boolean exit code: 0 (is valid), 1 (is not valid)
456 ################################################################################
457 function is_valid_requirement_pattern() {
459 java8pattern='1\.[4-8](\.0)?(\.0_[0-9]+)?[*+]?'
460 java9pattern='(9|1[0-9])(-ea|[*+]|(\.[0-9]+){1,2}[*+]?)?'
461 # test matches either old Java versioning scheme (up to 1.8) or new scheme (starting with 9)
462 if [[ ${java_req} =~ ^(${java8pattern}|${java9pattern})$ ]]; then
471 # determine which JVM to use
472 ############################################
474 # default Apple JRE plugin path (< 1.6)
475 apple_jre_plugin="/Library/Java/Home/bin/java"
476 apple_jre_version=$(get_java_version_from_cmd "${apple_jre_plugin}")
477 # default Oracle JRE plugin path (>= 1.7)
478 oracle_jre_plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"
479 oracle_jre_version=$(get_java_version_from_cmd "${oracle_jre_plugin}")
482 # first check system variable "$JAVA_HOME" -> has precedence over any other System JVM
483 stub_logger '[JavaSearch] Checking for $JAVA_HOME ...'
484 if [ -n "$JAVA_HOME" ] ; then
485 stub_logger "[JavaSearch] ... found JAVA_HOME with value $JAVA_HOME"
487 # PR 26: Allow specifying "$JAVA_HOME" relative to "$AppPackageFolder"
488 # which allows for bundling a custom version of Java inside your app!
489 if [[ $JAVA_HOME == /* ]] ; then
490 # if "$JAVA_HOME" starts with a Slash it's an absolute path
491 JAVACMD="$JAVA_HOME/bin/java"
493 # otherwise it's a relative path to "$AppPackageFolder"
494 JAVACMD="$AppPackageFolder/$JAVA_HOME/bin/java"
496 JAVACMD_version=$(get_comparable_java_version $(get_java_version_from_cmd "${JAVACMD}"))
498 stub_logger "[JavaSearch] ... didn't found JAVA_HOME"
502 # check for any other or a specific Java version
503 # also if $JAVA_HOME exists but isn't executable
504 if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
505 stub_logger "[JavaSearch] Checking for JavaVirtualMachines on the system ..."
510 # first check whether JVMVersion string is a valid requirement string
511 if [ ! -z "${JVMVersion}" ] && ! is_valid_requirement_pattern ${JVMVersion} ; then
512 MSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf "${MSG_JVMVERSION_REQ_INVALID}" "${JVMVersion}")
514 stub_logger "[EXIT 4] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"
515 # display error message with AppleScript
516 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)"
520 # then check whether JVMMaxVersion string is a valid requirement string
521 if [ ! -z "${JVMMaxVersion}" ] && ! is_valid_requirement_pattern ${JVMMaxVersion} ; then
522 MSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf "${MSG_JVMVERSION_REQ_INVALID}" "${JVMMaxVersion}")
524 stub_logger "[EXIT 5] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"
525 # display error message with AppleScript
526 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 # find installed JavaVirtualMachines (JDK + JRE)
534 # read JDK's from '/usr/libexec/java_home -V' command
535 while read -r line; do
536 version=$(echo $line | awk -F $',' '{print $1;}')
537 path=$(echo $line | awk -F $'" ' '{print $2;}')
539 allJVMs+=("$version:$path")
540 done < <(/usr/libexec/java_home -V 2>&1 | grep '^[[:space:]]')
541 # unset while loop variables
544 # add Apple JRE if available
545 if [ -x "${apple_jre_plugin}" ] ; then
546 allJVMs+=("$apple_jre_version:$apple_jre_plugin")
549 # add Oracle JRE if available
550 if [ -x "${oracle_jre_plugin}" ] ; then
551 allJVMs+=("$oracle_jre_version:$oracle_jre_plugin")
555 for i in "${allJVMs[@]}"
557 stub_logger "[JavaSearch] ... found JVM: $i"
561 # determine JVMs matching the min/max version requirement
562 minC=$(get_comparable_java_version ${JVMVersion})
563 maxC=$(get_comparable_java_version ${JVMMaxVersion})
566 for i in "${allJVMs[@]}"
568 # split JVM string at ':' delimiter to retain spaces in $path substring
569 IFS=: arr=($i) ; unset IFS
570 # [0] JVM version number
572 # comparable JVM version number
573 comp=$(get_comparable_java_version $ver)
576 # construct string item for adding to the "matchingJVMs" array
577 item="$comp:$ver:$path"
579 # pre-requisite: current version number needs to be greater than min version number
580 if [ "$comp" -ge "$minC" ] ; then
582 # perform max version checks if max version requirement is present
583 if [ ! -z ${JVMMaxVersion} ] ; then
585 # max version requirement ends with '*' modifier
586 if [[ ${JVMMaxVersion} == *\* ]] ; then
588 # use the '*' modifier from the max version string as wildcard for a 'starts with' comparison
589 # and check whether the current version number starts with the max version wildcard string
590 if [[ ${ver} == ${JVMMaxVersion} ]]; then
591 matchingJVMs+=("$item")
593 # or whether the current comparable version is lower than the comparable max version
594 elif [ "$comp" -le "$maxC" ] ; then
595 matchingJVMs+=("$item")
598 # max version requirement ends with '+' modifier -> always add this version if it's greater than $min
599 # because a max requirement with + modifier doesn't make sense
600 elif [[ ${JVMMaxVersion} == *+ ]] ; then
601 matchingJVMs+=("$item")
603 # matches 6 zeros at the end of the max version string (e.g. for 1.8, 9)
604 # -> then the max version string should be treated like with a '*' modifier at the end
605 #elif [[ ${maxC} =~ ^[0-9]{2}0{6}$ ]] && [ "$comp" -le $(( ${maxC#0} + 999 )) ] ; then
606 # matchingJVMs+=("$item")
608 # matches 3 zeros at the end of the max version string (e.g. for 9.1, 10.3)
609 # -> then the max version string should be treated like with a '*' modifier at the end
610 #elif [[ ${maxC} =~ ^[0-9]{5}0{3}$ ]] && [ "$comp" -le "${maxC}" ] ; then
611 # matchingJVMs+=("$item")
613 # matches standard requirements without modifier
614 elif [ "$comp" -le "$maxC" ]; then
615 matchingJVMs+=("$item")
618 # no max version requirement:
620 # min version requirement ends with '+' modifier
621 # -> always add the current version because it's greater than $min
622 elif [[ ${JVMVersion} == *+ ]] ; then
623 matchingJVMs+=("$item")
625 # min version requirement ends with '*' modifier
626 # -> use the '*' modifier from the min version string as wildcard for a 'starts with' comparison
627 # and check whether the current version number starts with the min version wildcard string
628 elif [[ ${JVMVersion} == *\* ]] ; then
629 if [[ ${ver} == ${JVMVersion} ]] ; then
630 matchingJVMs+=("$item")
633 # compare the min version against the current version with an additional * wildcard for a 'starts with' comparison
634 # -> e.g. add 1.8.0_44 when the requirement is 1.8
635 elif [[ ${ver} == ${JVMVersion}* ]] ; then
636 matchingJVMs+=("$item")
640 # unset for loop variables
641 unset arr ver comp path item
644 for i in "${matchingJVMs[@]}"
646 stub_logger "[JavaSearch] ... ... matches all requirements: $i"
650 # sort the matching JavaVirtualMachines by version number
651 # https://stackoverflow.com/a/11789688/1128689
652 IFS=$'\n' matchingJVMs=($(sort -nr <<<"${matchingJVMs[*]}"))
656 # get the highest matching JVM
657 for ((i = 0; i < ${#matchingJVMs[@]}; i++));
659 # split JVM string at ':' delimiter to retain spaces in $path substring
660 IFS=: arr=(${matchingJVMs[$i]}) ; unset IFS
661 # [0] comparable JVM version number
663 # [1] JVM version number
668 # use current value as JAVACMD if it's executable
669 if [ -x "$path" ] ; then
671 JAVACMD_version=$comp
675 # unset for loop variables
676 unset arr comp ver path
679 # log the Java Command and the extracted version number
680 stub_logger "[JavaCommand] '$JAVACMD'"
681 stub_logger "[JavaVersion] $(get_java_version_from_cmd "${JAVACMD}")${JAVACMD_version:+ / $JAVACMD_version}"
685 if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
687 # different error messages when a specific JVM was required
688 if [ ! -z "${JVMVersion}" ] ; then
689 # display human readable java version (#28)
690 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}/")
691 MSG_NO_SUITABLE_JAVA_EXPANDED=$(printf "${MSG_NO_SUITABLE_JAVA}" "${java_version_hr}").
693 if [ ! -z "${JVMMaxVersion}" ] ; then
694 java_version_hr=$(extract_java_major_version ${JVMVersion})
695 java_version_max_hr=$(echo ${JVMMaxVersion} | sed -E 's/^1\.([0-9+*]+)$/ \1/g' | sed "s/+//;s/*/ ${MSG_JAVA_VERSION_LATEST}/")
696 MSG_NO_SUITABLE_JAVA_EXPANDED="$(printf "${MSG_NO_SUITABLE_JAVA}" "${java_version_hr}") $(printf "${MSG_JAVA_VERSION_MAX}" "${java_version_max_hr}")"
700 stub_logger "[EXIT 3] ${MSG_NO_SUITABLE_JAVA_EXPANDED}"
702 # display error message with AppleScript
703 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)" \
704 -e "set response to button returned of the result" \
705 -e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" then open location \"http://java.com\""
711 stub_logger "[EXIT 1] ${MSG_ERROR_LAUNCHING}"
712 # display error message with AppleScript
713 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)" \
714 -e "set response to button returned of the result" \
715 -e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" then open location \"http://java.com\""
724 ############################################
726 if [ -z "${JVMMainClass}" ]; then
728 stub_logger "[EXIT 2] ${MSG_MISSING_MAINCLASS}"
729 # display error message with AppleScript
730 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)"
737 # execute $JAVACMD and do some preparation
738 ############################################
740 # enable drag&drop to the dock icon
741 export CFProcessPath="$0"
743 # remove Apples ProcessSerialNumber from passthru arguments (#39)
744 if [[ "$*" == -psn* ]] ; then
750 # change to Working Directory based upon Apple/Oracle Plist info
751 cd "${WorkingDirectory}" || exit 13
752 stub_logger "[WorkingDirectory] ${WorkingDirectory}"
754 # execute Java and set
759 # - JVM options / properties (-D)
760 # - JVM default options (-X)
762 # - main class arguments
763 # - passthrough arguments from Terminal or Drag'n'Drop to Finder icon
764 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[@]}")}"
766 -Djava.library.path="${AppleJavaFolder}" \
767 -cp "${JVMClassPath}" \
768 -splash:"${ResourcesFolder}/${JVMSplashFile}" \
769 -Xdock:icon="${ResourcesFolder}/${CFBundleIconFile}" \
770 -Xdock:name="${CFBundleName}" \
771 ${JVMOptionsArr:+"${JVMOptionsArr[@]}" }\
772 ${JVMDefaultOptions:+$JVMDefaultOptions }\
774 ${MainArgsArr:+ "${MainArgsArr[@]}"}\
775 ${ArgsPassthru:+ "${ArgsPassthru[@]}"}