Switch to universal JavaApplicationStub for Mac OS X
[fw/altos] / micropeak / MicroPeak.app / Contents / MacOS / JavaApplicationStub
1 #!/bin/bash
2 ##################################################################################
3 #                                                                                #
4 # universalJavaApplicationStub                                                   #
5 #                                                                                #
6 # A BASH based JavaApplicationStub for Java Apps on Mac OS X                     #
7 # that works with both Apple's and Oracle's plist format.                        #
8 #                                                                                #
9 # Inspired by Ian Roberts stackoverflow answer                                   #
10 # at http://stackoverflow.com/a/17546508/1128689                                 #
11 #                                                                                #
12 # @author    Tobias Fischer                                                      #
13 # @url       https://github.com/tofi86/universalJavaApplicationStub              #
14 # @date      2018-08-24                                                          #
15 # @version   3.0.4                                                               #
16 #                                                                                #
17 ##################################################################################
18 #                                                                                #
19 # The MIT License (MIT)                                                          #
20 #                                                                                #
21 # Copyright (c) 2014-2018 Tobias Fischer                                         #
22 #                                                                                #
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:                       #
29 #                                                                                #
30 # The above copyright notice and this permission notice shall be included in all #
31 # copies or substantial portions of the Software.                                #
32 #                                                                                #
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  #
39 # SOFTWARE.                                                                      #
40 #                                                                                #
41 ##################################################################################
42
43
44
45 # function 'stub_logger()'
46 #
47 # A logger which logs to the macOS Console.app using the 'syslog' command
48 #
49 # @param1  the log message
50 # @return  void
51 ################################################################################
52 function stub_logger() {
53         syslog -s -k \
54                 Facility com.apple.console \
55                 Level Notice \
56                 Sender "$(basename "$0")" \
57                 Message "[$$][${CFBundleName:-$(basename "$0")}] $1"
58 }
59
60
61
62 # set the directory abspath of the current
63 # shell script with symlinks being resolved
64 ############################################
65
66 PRG=$0
67 while [ -h "$PRG" ]; do
68         ls=$(ls -ld "$PRG")
69         link=$(expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null)
70         if expr "$link" : '^/' 2> /dev/null >/dev/null; then
71                 PRG="$link"
72         else
73                 PRG="$(dirname "$PRG")/$link"
74         fi
75 done
76 PROGDIR=$(dirname "$PRG")
77 stub_logger "[StubDir] $PROGDIR"
78
79
80
81 # set files and folders
82 ############################################
83
84 # the absolute path of the app package
85 cd "$PROGDIR"/../../ || exit 11
86 AppPackageFolder=$(pwd)
87
88 # the base path of the app package
89 cd .. || exit 12
90 AppPackageRoot=$(pwd)
91
92 # set Apple's Java folder
93 AppleJavaFolder="${AppPackageFolder}"/Contents/Resources/Java
94
95 # set Apple's Resources folder
96 AppleResourcesFolder="${AppPackageFolder}"/Contents/Resources
97
98 # set Oracle's Java folder
99 OracleJavaFolder="${AppPackageFolder}"/Contents/Java
100
101 # set Oracle's Resources folder
102 OracleResourcesFolder="${AppPackageFolder}"/Contents/Resources
103
104 # set path to Info.plist in bundle
105 InfoPlistFile="${AppPackageFolder}"/Contents/Info.plist
106
107 # set the default JVM Version to a null string
108 JVMVersion=""
109 JVMMaxVersion=""
110
111
112
113 # function 'plist_get()'
114 #
115 # read a specific Plist key with 'PlistBuddy' utility
116 #
117 # @param1  the Plist key with leading colon ':'
118 # @return  the value as String or Array
119 ################################################################################
120 plist_get(){
121         /usr/libexec/PlistBuddy -c "print $1" "${InfoPlistFile}" 2> /dev/null
122 }
123
124 # function 'plist_get_java()'
125 #
126 # read a specific Plist key with 'PlistBuddy' utility
127 # in the 'Java' or 'JavaX' dictionary (<dict>)
128 #
129 # @param1  the Plist :Java(X):Key with leading colon ':'
130 # @return  the value as String or Array
131 ################################################################################
132 plist_get_java(){
133         plist_get ${JavaKey:-":Java"}$1
134 }
135
136
137
138 # read Info.plist and extract JVM options
139 ############################################
140
141 # read the program name from CFBundleName
142 CFBundleName=$(plist_get ':CFBundleName')
143
144 # read the icon file name
145 CFBundleIconFile=$(plist_get ':CFBundleIconFile')
146
147
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
150 exitcode=$?
151 JavaKey=":Java"
152
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
156         exitcode=$?
157         JavaKey=":JavaX"
158 fi
159
160
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"
164
165         # set Java and Resources folder
166         JavaFolder="${AppleJavaFolder}"
167         ResourcesFolder="${AppleResourcesFolder}"
168
169         APP_PACKAGE="${AppPackageFolder}"
170         JAVAROOT="${AppleJavaFolder}"
171         USER_HOME="$HOME"
172
173
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}"
179         else
180                 # AppPackageRoot is the standard WorkingDirectory when the script is started
181                 WorkingDirectory="${AppPackageRoot}"
182         fi
183         # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
184         WorkingDirectory=$(eval echo "${WorkingDirectory}")
185
186
187         # read the MainClass name
188         JVMMainClass="$(plist_get_java ':MainClass')"
189
190         # read the SplashFile name
191         JVMSplashFile=$(plist_get_java ':SplashFile')
192
193         # read the JVM Properties as an array and retain spaces
194         IFS=$'\t\n'
195         JVMOptions=($(xargs -n1 <<<$(plist_get_java ':Properties' | grep " =" | sed 's/^ */-D/g' | sed -E 's/ = (.*)$/="\1"/g')))
196         unset IFS
197         # post processing of the array follows further below...
198
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)
203         else
204                 JVMClassPath=${JVMClassPath_RAW}
205         fi
206         # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
207         JVMClassPath=$(eval echo "${JVMClassPath}")
208
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)
213         else
214                 JVMDefaultOptions=${JVMDefaultOptions_RAW}
215         fi
216
217         # read StartOnMainThread and add as -XstartOnFirstThread
218         JVMStartOnMainThread=$(plist_get_java ':StartOnMainThread')
219         if [ "${JVMStartOnMainThread}" == "true" ]; then
220                 JVMDefaultOptions+=" -XstartOnFirstThread"
221         fi
222
223         # read the JVM Arguments as an array and retain spaces
224         IFS=$'\t\n'
225         MainArgs=($(xargs -n1 <<<$(plist_get_java ':Arguments')))
226         unset IFS
227         # post processing of the array follows further below...
228
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...
232
233
234 # read 'Info.plist' file in Oracle style
235 else
236         stub_logger "[PlistStyle] Oracle"
237
238         # set Working Directory and Java and Resources folder
239         JavaFolder="${OracleJavaFolder}"
240         ResourcesFolder="${OracleResourcesFolder}"
241         WorkingDirectory="${OracleJavaFolder}"
242
243         APP_ROOT="${AppPackageFolder}"
244
245         # read the MainClass name
246         JVMMainClass="$(plist_get ':JVMMainClassName')"
247
248         # read the SplashFile name
249         JVMSplashFile=$(plist_get ':JVMSplashFile')
250
251         # read the JVM Options as an array and retain spaces
252         IFS=$'\t\n'
253         JVMOptions=($(plist_get ':JVMOptions' | grep "    " | sed 's/^ *//g'))
254         unset IFS
255         # post processing of the array follows further below...
256
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}")
263
264         elif [[ ! -z ${JVMClassPath_RAW} ]] ; then
265                 JVMClassPath=${JVMClassPath_RAW}
266                 # expand variables $APP_PACKAGE, $JAVAROOT, $USER_HOME
267                 JVMClassPath=$(eval echo "${JVMClassPath}")
268
269         else
270                 #default: fallback to OracleJavaFolder
271                 JVMClassPath="${JavaFolder}/*"
272                 # Do NOT expand the default 'AppName.app/Contents/Java/*' classpath (#42)
273         fi
274
275         # read the JVM Default Options
276         JVMDefaultOptions=$(plist_get ':JVMDefaultOptions' | grep -o " \-.*" | tr -d '\n' | xargs)
277
278         # read the Main Arguments from JVMArguments key as an array and retain spaces (see #46 for naming details)
279         IFS=$'\t\n'
280         MainArgs=($(xargs -n1 <<<$(plist_get ':JVMArguments' | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/  */ /g')))
281         unset IFS
282         # post processing of the array follows further below...
283
284         # read the Java version we want to find
285         JVMVersion=$(plist_get ':JVMVersion' | xargs)
286         # post processing of the version string follows below...
287 fi
288
289
290
291 # JVMVersion: post processing and optional splitting
292 if [[ ${JVMVersion} == *";"* ]]; then
293         minMaxArray=(${JVMVersion//;/ })
294         JVMVersion=${minMaxArray[0]//+}
295         JVMMaxVersion=${minMaxArray[1]//+}
296 fi
297 stub_logger "[JavaRequirement] JVM minimum version: ${JVMVersion}"
298 stub_logger "[JavaRequirement] JVM maximum version: ${JVMMaxVersion}"
299
300 # MainArgs: replace occurences of $APP_ROOT with its content
301 MainArgsArr=()
302 for i in "${MainArgs[@]}"
303 do
304         MainArgsArr+=("$(eval echo "$i")")
305 done
306
307 # JVMOptions: replace occurences of $APP_ROOT with its content
308 JVMOptionsArr=()
309 for i in "${JVMOptions[@]}"
310 do
311         JVMOptionsArr+=("$(eval echo "$i")")
312 done
313
314
315 # internationalized messages
316 ############################################
317
318 LANG=$(defaults read -g AppleLocale)
319 stub_logger "[Language] $LANG"
320
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"
334
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."
346         MSG_LATER="Später"
347         MSG_VISIT_JAVA_DOT_COM="java.com öffnen"
348
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 了解如何安装。"
360         MSG_LATER="稍后"
361         MSG_VISIT_JAVA_DOT_COM="访问 java.com"
362
363 # English default localization
364 else
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..."
374         MSG_LATER="Later"
375         MSG_VISIT_JAVA_DOT_COM="Visit java.com"
376 fi
377
378
379
380 # function 'get_java_version_from_cmd()'
381 #
382 # returns Java version string from 'java -version' command
383 # works for both old (1.8) and new (9) version schema
384 #
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')
391 }
392
393
394 # function 'extract_java_major_version()'
395 #
396 # extract Java major version from a version string
397 #
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/')
403 }
404
405
406 # function 'get_comparable_java_version()'
407 #
408 # return comparable version for a Java version number or requirement string
409 #
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]})"
420 }
421
422
423 # function 'is_valid_requirement_pattern()'
424 #
425 # check whether the Java requirement is a valid requirement pattern
426 #
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)
444 #
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
453 #
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() {
458         local java_req=$1
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
463                 return 0
464         else
465                 return 1
466         fi
467 }
468
469
470
471 # determine which JVM to use
472 ############################################
473
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}")
480
481
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"
486
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"
492         else
493                 # otherwise it's a relative path to "$AppPackageFolder"
494                 JAVACMD="$AppPackageFolder/$JAVA_HOME/bin/java"
495         fi
496         JAVACMD_version=$(get_comparable_java_version $(get_java_version_from_cmd "${JAVACMD}"))
497 else
498         stub_logger "[JavaSearch] ... didn't found JAVA_HOME"
499 fi
500
501
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 ..."
506         # reset variables
507         JAVACMD=""
508         JAVACMD_version=""
509
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}")
513                 # log exit cause
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)"
517                 # exit with error
518                 exit 4
519         fi
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}")
523                 # log exit cause
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)"
527                 # exit with error
528                 exit 5
529         fi
530
531
532         # find installed JavaVirtualMachines (JDK + JRE)
533         allJVMs=()
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;}')
538                 path+="/bin/java"
539                 allJVMs+=("$version:$path")
540         done < <(/usr/libexec/java_home -V 2>&1 | grep '^[[:space:]]')
541         # unset while loop variables
542         unset version path
543
544         # add Apple JRE if available
545         if [ -x "${apple_jre_plugin}" ] ; then
546                 allJVMs+=("$apple_jre_version:$apple_jre_plugin")
547         fi
548
549         # add Oracle JRE if available
550         if [ -x "${oracle_jre_plugin}" ] ; then
551                 allJVMs+=("$oracle_jre_version:$oracle_jre_plugin")
552         fi
553
554         # debug output
555         for i in "${allJVMs[@]}"
556         do
557                 stub_logger "[JavaSearch] ... found JVM: $i"
558         done
559
560
561         # determine JVMs matching the min/max version requirement
562         minC=$(get_comparable_java_version ${JVMVersion})
563         maxC=$(get_comparable_java_version ${JVMMaxVersion})
564         matchingJVMs=()
565
566         for i in "${allJVMs[@]}"
567         do
568                 # split JVM string at ':' delimiter to retain spaces in $path substring
569                 IFS=: arr=($i) ; unset IFS
570                 # [0] JVM version number
571                 ver=${arr[0]}
572                 # comparable JVM version number
573                 comp=$(get_comparable_java_version $ver)
574                 # [1] JVM path
575                 path="${arr[1]}"
576                 # construct string item for adding to the "matchingJVMs" array
577                 item="$comp:$ver:$path"
578
579                 # pre-requisite: current version number needs to be greater than min version number
580                 if [ "$comp" -ge "$minC" ] ; then
581
582                         # perform max version checks if max version requirement is present
583                         if [ ! -z ${JVMMaxVersion} ] ; then
584
585                                 # max version requirement ends with '*' modifier
586                                 if [[ ${JVMMaxVersion} == *\* ]] ; then
587
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")
592
593                                         # or whether the current comparable version is lower than the comparable max version
594                                         elif [ "$comp" -le "$maxC" ] ; then
595                                                 matchingJVMs+=("$item")
596                                         fi
597
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")
602
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")
607
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")
612
613                                 # matches standard requirements without modifier
614                                 elif [ "$comp" -le "$maxC" ]; then
615                                         matchingJVMs+=("$item")
616                                 fi
617
618                         # no max version requirement:
619
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")
624
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")
631                                 fi
632
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")
637                         fi
638                 fi
639         done
640         # unset for loop variables
641         unset arr ver comp path item
642
643         # debug output
644         for i in "${matchingJVMs[@]}"
645         do
646                 stub_logger "[JavaSearch] ... ... matches all requirements: $i"
647         done
648
649
650         # sort the matching JavaVirtualMachines by version number
651         # https://stackoverflow.com/a/11789688/1128689
652         IFS=$'\n' matchingJVMs=($(sort -nr <<<"${matchingJVMs[*]}"))
653         unset IFS
654
655
656         # get the highest matching JVM
657         for ((i = 0; i < ${#matchingJVMs[@]}; i++));
658         do
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
662                 comp=${arr[0]}
663                 # [1] JVM version number
664                 ver=${arr[1]}
665                 # [2] JVM path
666                 path="${arr[2]}"
667
668                 # use current value as JAVACMD if it's executable
669                 if [ -x "$path" ] ; then
670                         JAVACMD="$path"
671                         JAVACMD_version=$comp
672                         break
673                 fi
674         done
675         # unset for loop variables
676         unset arr comp ver path
677 fi
678
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}"
682
683
684
685 if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
686
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}").
692
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}")"
697                 fi
698
699                 # log exit cause
700                 stub_logger "[EXIT 3] ${MSG_NO_SUITABLE_JAVA_EXPANDED}"
701
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\""
706                 # exit with error
707                 exit 3
708
709         else
710                 # log exit cause
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\""
716                 # exit with error
717                 exit 1
718         fi
719 fi
720
721
722
723 # MainClass check
724 ############################################
725
726 if [ -z "${JVMMainClass}" ]; then
727         # log exit cause
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)"
731         # exit with error
732         exit 2
733 fi
734
735
736
737 # execute $JAVACMD and do some preparation
738 ############################################
739
740 # enable drag&drop to the dock icon
741 export CFProcessPath="$0"
742
743 # remove Apples ProcessSerialNumber from passthru arguments (#39)
744 if [[ "$*" == -psn* ]] ; then
745         ArgsPassthru=()
746 else
747         ArgsPassthru=("$@")
748 fi
749
750 # change to Working Directory based upon Apple/Oracle Plist info
751 cd "${WorkingDirectory}" || exit 13
752 stub_logger "[WorkingDirectory] ${WorkingDirectory}"
753
754 # execute Java and set
755 # - classpath
756 # - splash image
757 # - dock icon
758 # - app name
759 # - JVM options / properties (-D)
760 # - JVM default options (-X)
761 # - main class
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[@]}")}"
765 exec "${JAVACMD}" \
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 }\
773                 "${JVMMainClass}"\
774                 ${MainArgsArr:+ "${MainArgsArr[@]}"}\
775                 ${ArgsPassthru:+ "${ArgsPassthru[@]}"}