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      2021-02-21                                                          #
15# @version   3.2.0                                                               #
16#                                                                                #
17##################################################################################
18#                                                                                #
19# The MIT License (MIT)                                                          #
20#                                                                                #
21# Copyright (c) 2014-2021 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################################################################################
52function 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
66PRG=$0
67while [ -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
75done
76PROGDIR=$(dirname "$PRG")
77stub_logger "[StubDir] $PROGDIR"
78
79
80
81# set files and folders
82############################################
83
84# the absolute path of the app package
85cd "$PROGDIR"/../../ || exit 11
86AppPackageFolder=$(pwd)
87
88# the base path of the app package
89cd .. || exit 12
90AppPackageRoot=$(pwd)
91
92# set Apple's Java folder
93AppleJavaFolder="${AppPackageFolder}"/Contents/Resources/Java
94
95# set Apple's Resources folder
96AppleResourcesFolder="${AppPackageFolder}"/Contents/Resources
97
98# set Oracle's Java folder
99OracleJavaFolder="${AppPackageFolder}"/Contents/Java
100
101# set Oracle's Resources folder
102OracleResourcesFolder="${AppPackageFolder}"/Contents/Resources
103
104# set path to Info.plist in bundle
105InfoPlistFile="${AppPackageFolder}"/Contents/Info.plist
106
107# set the default JVM Version to a null string
108JVMVersion=""
109JVMMaxVersion=""
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################################################################################
120plist_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################################################################################
132plist_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
142CFBundleName=$(plist_get ':CFBundleName')
143
144# read the icon file name
145CFBundleIconFile=$(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
150exitcode=$?
151JavaKey=":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
154if [ $exitcode -ne 0 ]; then
155	/usr/libexec/PlistBuddy -c "print :JavaX" "${InfoPlistFile}" > /dev/null 2>&1
156	exitcode=$?
157	JavaKey=":JavaX"
158fi
159
160
161# read 'Info.plist' file in Apple style if exit code returns 0 (true, ':Java' key is present)
162if [ $exitcode -eq 0 ]; then
163	stub_logger "[PlistStyle] Apple"
164
165	# set Java and Resources folder
166	JavaFolder="${AppleJavaFolder}"
167	ResourcesFolder="${AppleResourcesFolder}"
168
169	# set expandable variables
170	APP_ROOT="${AppPackageFolder}"
171	APP_PACKAGE="${AppPackageFolder}"
172	JAVAROOT="${AppleJavaFolder}"
173	USER_HOME="$HOME"
174
175
176	# read the Java WorkingDirectory
177	JVMWorkDir=$(plist_get_java ':WorkingDirectory' | xargs)
178	# set Working Directory based upon PList value
179	if [[ ! -z ${JVMWorkDir} ]]; then
180		WorkingDirectory="${JVMWorkDir}"
181	else
182		# AppPackageRoot is the standard WorkingDirectory when the script is started
183		WorkingDirectory="${AppPackageRoot}"
184	fi
185	# expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
186	WorkingDirectory=$(eval echo "${WorkingDirectory}")
187
188
189	# read the MainClass name
190	JVMMainClass="$(plist_get_java ':MainClass')"
191
192	# read the SplashFile name
193	JVMSplashFile=$(plist_get_java ':SplashFile')
194
195	# read the JVM Properties as an array and retain spaces
196	IFS=$'\t\n'
197	JVMOptions=($(xargs -n1 <<<$(plist_get_java ':Properties' | grep " =" | sed 's/^ */-D/g' | sed -E 's/ = (.*)$/="\1"/g')))
198	unset IFS
199	# post processing of the array follows further below...
200
201	# read the ClassPath in either Array or String style
202	JVMClassPath_RAW=$(plist_get_java ':ClassPath' | xargs)
203	if [[ $JVMClassPath_RAW == *Array* ]] ; then
204		JVMClassPath=.$(plist_get_java ':ClassPath' | grep "    " | sed 's/^ */:/g' | tr -d '\n' | xargs)
205	else
206		JVMClassPath=${JVMClassPath_RAW}
207	fi
208	# expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
209	JVMClassPath=$(eval echo "${JVMClassPath}")
210
211	# read the JVM Options in either Array or String style
212	JVMDefaultOptions_RAW=$(plist_get_java ':VMOptions' | xargs)
213	if [[ $JVMDefaultOptions_RAW == *Array* ]] ; then
214		JVMDefaultOptions=$(plist_get_java ':VMOptions' | grep "    " | sed 's/^ */ /g' | tr -d '\n' | xargs)
215	else
216		JVMDefaultOptions=${JVMDefaultOptions_RAW}
217	fi
218	# expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME (#84)
219	JVMDefaultOptions=$(eval echo "${JVMDefaultOptions}")
220
221	# read StartOnMainThread and add as -XstartOnFirstThread
222	JVMStartOnMainThread=$(plist_get_java ':StartOnMainThread')
223	if [ "${JVMStartOnMainThread}" == "true" ]; then
224		JVMDefaultOptions+=" -XstartOnFirstThread"
225	fi
226
227	# read the JVM Arguments in either Array or String style (#76) and retain spaces
228	IFS=$'\t\n'
229	MainArgs_RAW=$(plist_get_java ':Arguments' | xargs)
230	if [[ $MainArgs_RAW == *Array* ]] ; then
231		MainArgs=($(xargs -n1 <<<$(plist_get_java ':Arguments' | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/  */ /g')))
232	else
233		MainArgs=($(xargs -n1 <<<$(plist_get_java ':Arguments')))
234	fi
235	unset IFS
236	# post processing of the array follows further below...
237
238	# read the Java version we want to find
239	JVMVersion=$(plist_get_java ':JVMVersion' | xargs)
240	# post processing of the version string follows below...
241
242
243# read 'Info.plist' file in Oracle style
244else
245	stub_logger "[PlistStyle] Oracle"
246
247	# set Working Directory and Java and Resources folder
248	JavaFolder="${OracleJavaFolder}"
249	ResourcesFolder="${OracleResourcesFolder}"
250	WorkingDirectory="${OracleJavaFolder}"
251
252	# set expandable variables
253	APP_ROOT="${AppPackageFolder}"
254	APP_PACKAGE="${AppPackageFolder}"
255	JAVAROOT="${OracleJavaFolder}"
256	USER_HOME="$HOME"
257
258	# read the MainClass name
259	JVMMainClass="$(plist_get ':JVMMainClassName')"
260
261	# read the SplashFile name
262	JVMSplashFile=$(plist_get ':JVMSplashFile')
263
264	# read the JVM Options as an array and retain spaces
265	IFS=$'\t\n'
266	JVMOptions=($(plist_get ':JVMOptions' | grep "    " | sed 's/^ *//g'))
267	unset IFS
268	# post processing of the array follows further below...
269
270	# read the ClassPath in either Array or String style
271	JVMClassPath_RAW=$(plist_get ':JVMClassPath')
272	if [[ $JVMClassPath_RAW == *Array* ]] ; then
273		JVMClassPath=.$(plist_get ':JVMClassPath' | grep "    " | sed 's/^ */:/g' | tr -d '\n' | xargs)
274		# expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
275		JVMClassPath=$(eval echo "${JVMClassPath}")
276
277	elif [[ ! -z ${JVMClassPath_RAW} ]] ; then
278		JVMClassPath=${JVMClassPath_RAW}
279		# expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
280		JVMClassPath=$(eval echo "${JVMClassPath}")
281
282	else
283		#default: fallback to OracleJavaFolder
284		JVMClassPath="${JavaFolder}/*"
285		# Do NOT expand the default 'AppName.app/Contents/Java/*' classpath (#42)
286	fi
287
288	# read the JVM Default Options by parsing the :JVMDefaultOptions <dict>
289	# and pulling all <string> values starting with a dash (-)
290	JVMDefaultOptions=$(plist_get ':JVMDefaultOptions' | grep -o " \-.*" | tr -d '\n' | xargs)
291	# expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME (#99)
292	JVMDefaultOptions=$(eval echo "${JVMDefaultOptions}")
293
294	# read the Main Arguments from JVMArguments key as an array and retain spaces (see #46 for naming details)
295	IFS=$'\t\n'
296	MainArgs=($(xargs -n1 <<<$(plist_get ':JVMArguments' | tr -d '\n' | sed -E 's/Array \{ *(.*) *\}/\1/g' | sed 's/  */ /g')))
297	unset IFS
298	# post processing of the array follows further below...
299
300	# read the Java version we want to find
301	JVMVersion=$(plist_get ':JVMVersion' | xargs)
302	# post processing of the version string follows below...
303fi
304
305
306# (#75) check for undefined icons or icon names without .icns extension and prepare
307# an osascript statement for those cases when the icon can be shown in the dialog
308DialogWithIcon=""
309if [ ! -z ${CFBundleIconFile} ]; then
310	if [[ ${CFBundleIconFile} == *.icns ]] && [[ -f "${ResourcesFolder}/${CFBundleIconFile}" ]] ; then
311		DialogWithIcon=" with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
312	elif [[ ${CFBundleIconFile} != *.icns ]] && [[ -f "${ResourcesFolder}/${CFBundleIconFile}.icns" ]] ; then
313		CFBundleIconFile+=".icns"
314		DialogWithIcon=" with icon path to resource \"${CFBundleIconFile}\" in bundle (path to me)"
315	fi
316fi
317
318
319# JVMVersion: post processing and optional splitting
320if [[ ${JVMVersion} == *";"* ]]; then
321	minMaxArray=(${JVMVersion//;/ })
322	JVMVersion=${minMaxArray[0]//+}
323	JVMMaxVersion=${minMaxArray[1]//+}
324fi
325stub_logger "[JavaRequirement] JVM minimum version: ${JVMVersion}"
326stub_logger "[JavaRequirement] JVM maximum version: ${JVMMaxVersion}"
327
328# MainArgs: expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
329MainArgsArr=()
330for i in "${MainArgs[@]}"
331do
332	MainArgsArr+=("$(eval echo "$i")")
333done
334
335# JVMOptions: expand variables $APP_PACKAGE, $APP_ROOT, $JAVAROOT, $USER_HOME
336JVMOptionsArr=()
337for i in "${JVMOptions[@]}"
338do
339	JVMOptionsArr+=("$(eval echo "$i")")
340done
341
342
343# internationalized messages
344############################################
345
346# supported languages / available translations
347stubLanguages="^(fr|de|zh|es|en)-"
348
349# read user preferred languages as defined in macOS System Preferences (#101)
350stub_logger '[LanguageSearch] Checking preferred languages in macOS System Preferences...'
351appleLanguages=($(defaults read -g AppleLanguages | grep '\s"' | tr -d ',' | xargs))
352stub_logger "[LanguageSearch] ... found [${appleLanguages[*]}]"
353
354language=""
355for i in "${appleLanguages[@]}"
356do
357	langValue="${i%-*}"
358    if [[ "$i" =~ $stubLanguages ]]; then
359		stub_logger "[LanguageSearch] ... selected '$i' ('$langValue') as the default language for the launcher stub"
360		language=${langValue}
361        break
362	fi
363done
364if [ -z "${language}" ]; then
365    language="en"
366    stub_logger "[LanguageSearch] ... selected fallback 'en' as the default language for the launcher stub"
367fi
368stub_logger "[Language] $language"
369
370
371case "${language}" in
372# French
373fr)
374	MSG_ERROR_LAUNCHING="ERREUR au lancement de '${CFBundleName}'."
375	MSG_MISSING_MAINCLASS="'MainClass' n'est pas spécifié.\nL'application Java ne peut pas être lancée."
376	MSG_JVMVERSION_REQ_INVALID="La syntaxe de la version de Java demandée est invalide: %s\nVeuillez contacter le développeur de l'application."
377	MSG_NO_SUITABLE_JAVA="La version de Java installée sur votre système ne convient pas.\nCe programme nécessite Java %s"
378	MSG_JAVA_VERSION_OR_LATER="ou ultérieur"
379	MSG_JAVA_VERSION_LATEST="(dernière mise à jour)"
380	MSG_JAVA_VERSION_MAX="à %s"
381	MSG_NO_SUITABLE_JAVA_CHECK="Merci de bien vouloir installer la version de Java requise."
382	MSG_INSTALL_JAVA="Java doit être installé sur votre système.\nRendez-vous sur java.com et suivez les instructions d'installation..."
383	MSG_LATER="Plus tard"
384	MSG_VISIT_JAVA_DOT_COM="Java by Oracle"
385	MSG_VISIT_ADOPTOPENJDK="Java by AdoptOpenJDK"
386    ;;
387
388# German
389de)
390	MSG_ERROR_LAUNCHING="FEHLER beim Starten von '${CFBundleName}'."
391	MSG_MISSING_MAINCLASS="Die 'MainClass' ist nicht spezifiziert!\nDie Java-Anwendung kann nicht gestartet werden!"
392	MSG_JVMVERSION_REQ_INVALID="Die Syntax der angeforderten Java-Version ist ungültig: %s\nBitte kontaktieren Sie den Entwickler der App."
393	MSG_NO_SUITABLE_JAVA="Es wurde keine passende Java-Version auf Ihrem System gefunden!\nDieses Programm benötigt Java %s"
394	MSG_JAVA_VERSION_OR_LATER="oder neuer"
395	MSG_JAVA_VERSION_LATEST="(neuste Unterversion)"
396	MSG_JAVA_VERSION_MAX="bis %s"
397	MSG_NO_SUITABLE_JAVA_CHECK="Stellen Sie sicher, dass die angeforderte Java-Version installiert ist."
398	MSG_INSTALL_JAVA="Auf Ihrem System muss die 'Java'-Software installiert sein.\nBesuchen Sie java.com für weitere Installationshinweise."
399	MSG_LATER="Später"
400	MSG_VISIT_JAVA_DOT_COM="Java von Oracle"
401	MSG_VISIT_ADOPTOPENJDK="Java von AdoptOpenJDK"
402    ;;
403
404# Simplified Chinese
405zh)
406	MSG_ERROR_LAUNCHING="无法启动 '${CFBundleName}'."
407	MSG_MISSING_MAINCLASS="没有指定 'MainClass'!\nJava程序无法启动!"
408	MSG_JVMVERSION_REQ_INVALID="Java版本参数语法错误: %s\n请联系该应用的开发者。"
409	MSG_NO_SUITABLE_JAVA="没有在系统中找到合适的Java版本!\n必须安装Java %s才能够使用该程序!"
410	MSG_JAVA_VERSION_OR_LATER="及以上版本"
411	MSG_JAVA_VERSION_LATEST="(最新版本)"
412	MSG_JAVA_VERSION_MAX="最高为 %s"
413	MSG_NO_SUITABLE_JAVA_CHECK="请确保系统中安装了所需的Java版本"
414	MSG_INSTALL_JAVA="你需要在Mac中安装Java运行环境!\n访问 java.com 了解如何安装。"
415	MSG_LATER="稍后"
416	MSG_VISIT_JAVA_DOT_COM="Java by Oracle"
417	MSG_VISIT_ADOPTOPENJDK="Java by AdoptOpenJDK"
418    ;;
419
420# Spanish
421es)
422	MSG_ERROR_LAUNCHING="ERROR iniciando '${CFBundleName}'."
423	MSG_MISSING_MAINCLASS="¡'MainClass' no especificada!\n¡La aplicación Java no puede iniciarse!"
424	MSG_JVMVERSION_REQ_INVALID="La sintaxis de la versión Java requerida no es válida: %s\nPor favor, contacte con el desarrollador de la aplicación."
425	MSG_NO_SUITABLE_JAVA="¡No se encontró una versión de Java adecuada en su sistema!\nEste programa requiere Java %s"
426	MSG_JAVA_VERSION_OR_LATER="o posterior"
427	MSG_JAVA_VERSION_LATEST="(ultima actualización)"
428	MSG_JAVA_VERSION_MAX="superior a %s"
429	MSG_NO_SUITABLE_JAVA_CHECK="Asegúrese de instalar la versión Java requerida."
430	MSG_INSTALL_JAVA="¡Necesita tener JAVA instalado en su Mac!\nVisite java.com para consultar las instrucciones para su instalación..."
431	MSG_LATER="Más tarde"
432	MSG_VISIT_JAVA_DOT_COM="Java de Oracle"
433	MSG_VISIT_ADOPTOPENJDK="Java de AdoptOpenJDK"
434    ;;
435
436# English | default
437en|*)
438	MSG_ERROR_LAUNCHING="ERROR launching '${CFBundleName}'."
439	MSG_MISSING_MAINCLASS="'MainClass' isn't specified!\nJava application cannot be started!"
440	MSG_JVMVERSION_REQ_INVALID="The syntax of the required Java version is invalid: %s\nPlease contact the App developer."
441	MSG_NO_SUITABLE_JAVA="No suitable Java version found on your system!\nThis program requires Java %s"
442	MSG_JAVA_VERSION_OR_LATER="or later"
443	MSG_JAVA_VERSION_LATEST="(latest update)"
444	MSG_JAVA_VERSION_MAX="up to %s"
445	MSG_NO_SUITABLE_JAVA_CHECK="Make sure you install the required Java version."
446	MSG_INSTALL_JAVA="You need to have JAVA installed on your Mac!\nVisit java.com for installation instructions..."
447	MSG_LATER="Later"
448	MSG_VISIT_JAVA_DOT_COM="Java by Oracle"
449	MSG_VISIT_ADOPTOPENJDK="Java by AdoptOpenJDK"
450    ;;
451esac
452
453
454
455# function 'get_java_version_from_cmd()'
456#
457# returns Java version string from 'java -version' command
458# works for both old (1.8) and new (9) version schema
459#
460# @param1  path to a java JVM executable
461# @return  the Java version number as displayed in 'java -version' command
462################################################################################
463function get_java_version_from_cmd() {
464	# second sed command strips " and -ea from the version string
465	echo $("$1" -version 2>&1 | awk '/version/{print $3}' | sed -E 's/"//g;s/-ea//g')
466}
467
468
469# function 'extract_java_major_version()'
470#
471# extract Java major version from a version string
472#
473# @param1  a Java version number ('1.8.0_45') or requirement string ('1.8+')
474# @return  the major version (e.g. '7', '8' or '9', etc.)
475################################################################################
476function extract_java_major_version() {
477	echo $(echo "$1" | sed -E 's/^1\.//;s/^([0-9]+)(-ea|(\.[0-9_.]{1,7})?)(-b[0-9]+-[0-9]+)?[+*]?$/\1/')
478}
479
480
481# function 'get_comparable_java_version()'
482#
483# return comparable version for a Java version number or requirement string
484#
485# @param1  a Java version number ('1.8.0_45') or requirement string ('1.8+')
486# @return  an 8 digit numeral ('1.8.0_45'->'08000045'; '9.1.13'->'09001013')
487################################################################################
488function get_comparable_java_version() {
489	# cleaning: 1) remove leading '1.'; 2) remove build string (e.g. '-b14-468'); 3) remove 'a-Z' and '-*+' (e.g. '-ea'); 4) replace '_' with '.'
490	local cleaned=$(echo "$1" | sed -E 's/^1\.//g;s/-b[0-9]+-[0-9]+$//g;s/[a-zA-Z+*\-]//g;s/_/./g')
491	# splitting at '.' into an array
492	local arr=( ${cleaned//./ } )
493	# echo a string with left padded version numbers
494	echo "$(printf '%02s' ${arr[0]})$(printf '%03s' ${arr[1]})$(printf '%03s' ${arr[2]})"
495}
496
497
498# function 'is_valid_requirement_pattern()'
499#
500# check whether the Java requirement is a valid requirement pattern
501#
502# supported requirements are for example:
503# - 1.6       requires Java 6 (any update)      [1.6, 1.6.0_45, 1.6.0_88]
504# - 1.6*      requires Java 6 (any update)      [1.6, 1.6.0_45, 1.6.0_88]
505# - 1.6+      requires Java 6 or higher         [1.6, 1.6.0_45, 1.8, 9, etc.]
506# - 1.6.0     requires Java 6 (any update)      [1.6, 1.6.0_45, 1.6.0_88]
507# - 1.6.0_45  requires Java 6u45                [1.6.0_45]
508# - 1.6.0_45+ requires Java 6u45 or higher      [1.6.0_45, 1.6.0_88, 1.8, etc.]
509# - 9         requires Java 9 (any update)      [9.0.*, 9.1, 9.3, etc.]
510# - 9*        requires Java 9 (any update)      [9.0.*, 9.1, 9.3, etc.]
511# - 9+        requires Java 9 or higher         [9.0, 9.1, 10, etc.]
512# - 9.1       requires Java 9.1 (any update)    [9.1.*, 9.1.2, 9.1.13, etc.]
513# - 9.1*      requires Java 9.1 (any update)    [9.1.*, 9.1.2, 9.1.13, etc.]
514# - 9.1+      requires Java 9.1 or higher       [9.1, 9.2, 10, etc.]
515# - 9.1.3     requires Java 9.1.3               [9.1.3]
516# - 9.1.3*    requires Java 9.1.3 (any update)  [9.1.3]
517# - 9.1.3+    requires Java 9.1.3 or higher     [9.1.3, 9.1.4, 9.2.*, 10, etc.]
518# - 10-ea     requires Java 10 (early access release)
519#
520# unsupported requirement patterns are for example:
521# - 1.2, 1.3, 1.9       Java 2, 3 are not supported
522# - 1.9                 Java 9 introduced a new versioning scheme
523# - 6u45                known versioning syntax, but unsupported
524# - 9-ea*, 9-ea+        early access releases paired with */+
525# - 9., 9.*, 9.+        version ending with a .
526# - 9.1., 9.1.*, 9.1.+  version ending with a .
527# - 9.3.5.6             4 part version number is unsupported
528#
529# @param1  a Java requirement string ('1.8+')
530# @return  boolean exit code: 0 (is valid), 1 (is not valid)
531################################################################################
532function is_valid_requirement_pattern() {
533	local java_req=$1
534	java8pattern='1\.[4-8](\.[0-9]+)?(\.0_[0-9]+)?[*+]?'
535	java9pattern='(9|1[0-9])(-ea|[*+]|(\.[0-9]+){1,2}[*+]?)?'
536	# test matches either old Java versioning scheme (up to 1.8) or new scheme (starting with 9)
537	if [[ ${java_req} =~ ^(${java8pattern}|${java9pattern})$ ]]; then
538		return 0
539	else
540		return 1
541	fi
542}
543
544
545
546# determine which JVM to use
547############################################
548
549# default Apple JRE plugin path (< 1.6)
550apple_jre_plugin="/Library/Java/Home/bin/java"
551apple_jre_version=$(get_java_version_from_cmd "${apple_jre_plugin}")
552# default Oracle JRE plugin path (>= 1.7)
553oracle_jre_plugin="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"
554oracle_jre_version=$(get_java_version_from_cmd "${oracle_jre_plugin}")
555
556
557# first check system variable "$JAVA_HOME" -> has precedence over any other System JVM
558stub_logger '[JavaSearch] Checking for $JAVA_HOME ...'
559if [ -n "$JAVA_HOME" ] ; then
560	stub_logger "[JavaSearch] ... found JAVA_HOME with value $JAVA_HOME"
561
562	# PR 26: Allow specifying "$JAVA_HOME" relative to "$AppPackageFolder"
563	# which allows for bundling a custom version of Java inside your app!
564	if [[ $JAVA_HOME == /* ]] ; then
565		# if "$JAVA_HOME" starts with a Slash it's an absolute path
566		JAVACMD="$JAVA_HOME/bin/java"
567		stub_logger "[JavaSearch] ... parsing JAVA_HOME as absolute path to the executable '$JAVACMD'"
568	else
569		# otherwise it's a relative path to "$AppPackageFolder"
570		JAVACMD="$AppPackageFolder/$JAVA_HOME/bin/java"
571		stub_logger "[JavaSearch] ... parsing JAVA_HOME as relative path inside the App bundle to the executable '$JAVACMD'"
572	fi
573	JAVACMD_version=$(get_comparable_java_version $(get_java_version_from_cmd "${JAVACMD}"))
574else
575	stub_logger "[JavaSearch] ... haven't found JAVA_HOME"
576fi
577
578
579# check for any other or a specific Java version
580# also if $JAVA_HOME exists but isn't executable
581if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
582
583	# add a warning in the syslog if JAVA_HOME is not executable or not found (#100)
584	if [ -n "$JAVA_HOME" ] ; then
585		stub_logger "[JavaSearch] ... but no 'java' executable was found at the JAVA_HOME location!"
586	fi
587
588	stub_logger "[JavaSearch] Searching for JavaVirtualMachines on the system ..."
589	# reset variables
590	JAVACMD=""
591	JAVACMD_version=""
592
593	# first check whether JVMVersion string is a valid requirement string
594	if [ ! -z "${JVMVersion}" ] && ! is_valid_requirement_pattern ${JVMVersion} ; then
595		MSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf "${MSG_JVMVERSION_REQ_INVALID}" "${JVMVersion}")
596		# log exit cause
597		stub_logger "[EXIT 4] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"
598		# display error message with AppleScript
599		osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_JVMVERSION_REQ_INVALID_EXPANDED}\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1${DialogWithIcon}"
600		# exit with error
601		exit 4
602	fi
603	# then check whether JVMMaxVersion string is a valid requirement string
604	if [ ! -z "${JVMMaxVersion}" ] && ! is_valid_requirement_pattern ${JVMMaxVersion} ; then
605		MSG_JVMVERSION_REQ_INVALID_EXPANDED=$(printf "${MSG_JVMVERSION_REQ_INVALID}" "${JVMMaxVersion}")
606		# log exit cause
607		stub_logger "[EXIT 5] ${MSG_JVMVERSION_REQ_INVALID_EXPANDED}"
608		# display error message with AppleScript
609		osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_JVMVERSION_REQ_INVALID_EXPANDED}\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1${DialogWithIcon}"
610		# exit with error
611		exit 5
612	fi
613
614
615	# find installed JavaVirtualMachines (JDK + JRE)
616	allJVMs=()
617
618	# read JDK's from '/usr/libexec/java_home --xml' command with PlistBuddy and a custom Dict iterator
619	# idea: https://stackoverflow.com/a/14085460/1128689 and https://scriptingosx.com/2018/07/parsing-dscl-output-in-scripts/
620	javaXml=$(/usr/libexec/java_home --xml)
621	javaCounter=$(/usr/libexec/PlistBuddy -c "Print" /dev/stdin <<< $javaXml | grep "Dict" | wc -l | tr -d ' ')
622
623	# iterate over all Dict entries
624	# but only if there are any JVMs at all (#93)
625	if [ "$javaCounter" -gt "0" ] ; then
626		for idx in $(seq 0 $((javaCounter - 1)))
627		do
628			version=$(/usr/libexec/PlistBuddy -c "print :$idx:JVMVersion" /dev/stdin <<< $javaXml)
629			path=$(/usr/libexec/PlistBuddy -c "print :$idx:JVMHomePath" /dev/stdin <<< $javaXml)
630			path+="/bin/java"
631			allJVMs+=("$version:$path")
632		done
633		# unset for loop variables
634		unset version path
635	fi
636
637	# add SDKMAN! java versions (#95)
638	if [ -d ~/.sdkman/candidates/java/ ] ; then
639		for sdkjdk in ~/.sdkman/candidates/java/*/
640		do
641			if [[ ${sdkjdk} =~ /current/$ ]] ; then
642				continue
643			fi
644
645			sdkjdkcmd="${sdkjdk}bin/java"
646			version=$(get_java_version_from_cmd "${sdkjdkcmd}")
647			allJVMs+=("$version:$sdkjdkcmd")
648		done
649		# unset for loop variables
650		unset version
651	fi
652
653	# add Apple JRE if available
654	if [ -x "${apple_jre_plugin}" ] ; then
655		allJVMs+=("$apple_jre_version:$apple_jre_plugin")
656	fi
657
658	# add Oracle JRE if available
659	if [ -x "${oracle_jre_plugin}" ] ; then
660		allJVMs+=("$oracle_jre_version:$oracle_jre_plugin")
661	fi
662
663	# debug output
664	for i in "${allJVMs[@]}"
665	do
666		stub_logger "[JavaSearch] ... found JVM: $i"
667	done
668
669
670	# determine JVMs matching the min/max version requirement
671
672	stub_logger "[JavaSearch] Filtering the result list for JVMs matching the min/max version requirement ..."
673
674	minC=$(get_comparable_java_version ${JVMVersion})
675	maxC=$(get_comparable_java_version ${JVMMaxVersion})
676	matchingJVMs=()
677
678	for i in "${allJVMs[@]}"
679	do
680		# split JVM string at ':' delimiter to retain spaces in $path substring
681		IFS=: arr=($i) ; unset IFS
682		# [0] JVM version number
683		ver=${arr[0]}
684		# comparable JVM version number
685		comp=$(get_comparable_java_version $ver)
686		# [1] JVM path
687		path="${arr[1]}"
688		# construct string item for adding to the "matchingJVMs" array
689		item="$comp:$ver:$path"
690
691		# pre-requisite: current version number needs to be greater than min version number
692		if [ "$comp" -ge "$minC" ] ; then
693
694			# perform max version checks if max version requirement is present
695			if [ ! -z ${JVMMaxVersion} ] ; then
696
697				# max version requirement ends with '*' modifier
698				if [[ ${JVMMaxVersion} == *\* ]] ; then
699
700					# use the '*' modifier from the max version string as wildcard for a 'starts with' comparison
701					# and check whether the current version number starts with the max version wildcard string
702					if [[ ${ver} == ${JVMMaxVersion} ]]; then
703						matchingJVMs+=("$item")
704
705					# or whether the current comparable version is lower than the comparable max version
706					elif [ "$comp" -le "$maxC" ] ; then
707						matchingJVMs+=("$item")
708					fi
709
710				# max version requirement ends with '+' modifier -> always add this version if it's greater than $min
711				# because a max requirement with + modifier doesn't make sense
712				elif [[ ${JVMMaxVersion} == *+ ]] ; then
713					matchingJVMs+=("$item")
714
715				# matches 6 zeros at the end of the max version string (e.g. for 1.8, 9)
716				# -> then the max version string should be treated like with a '*' modifier at the end
717				#elif [[ ${maxC} =~ ^[0-9]{2}0{6}$ ]] && [ "$comp" -le $(( ${maxC#0} + 999 )) ] ; then
718				#	matchingJVMs+=("$item")
719
720				# matches 3 zeros at the end of the max version string (e.g. for 9.1, 10.3)
721				# -> then the max version string should be treated like with a '*' modifier at the end
722				#elif [[ ${maxC} =~ ^[0-9]{5}0{3}$ ]] && [ "$comp" -le "${maxC}" ] ; then
723				#	matchingJVMs+=("$item")
724
725				# matches standard requirements without modifier
726				elif [ "$comp" -le "$maxC" ]; then
727					matchingJVMs+=("$item")
728				fi
729
730			# no max version requirement:
731
732			# min version requirement ends with '+' modifier
733			# -> always add the current version because it's greater than $min
734			elif [[ ${JVMVersion} == *+ ]] ; then
735				matchingJVMs+=("$item")
736
737			# min version requirement ends with '*' modifier
738			# -> use the '*' modifier from the min version string as wildcard for a 'starts with' comparison
739			#    and check whether the current version number starts with the min version wildcard string
740			elif [[ ${JVMVersion} == *\* ]] ; then
741				if [[ ${ver} == ${JVMVersion} ]] ; then
742					matchingJVMs+=("$item")
743				fi
744
745			# compare the min version against the current version with an additional * wildcard for a 'starts with' comparison
746			# -> e.g. add 1.8.0_44 when the requirement is 1.8
747			elif [[ ${ver} == ${JVMVersion}* ]] ; then
748					matchingJVMs+=("$item")
749			fi
750		fi
751	done
752	# unset for loop variables
753	unset arr ver comp path item
754
755	# debug output
756	for i in "${matchingJVMs[@]}"
757	do
758		stub_logger "[JavaSearch] ... matches all requirements: $i"
759	done
760
761
762	# sort the matching JavaVirtualMachines by version number
763	# https://stackoverflow.com/a/11789688/1128689
764	IFS=$'\n' matchingJVMs=($(sort -nr <<<"${matchingJVMs[*]}"))
765	unset IFS
766
767
768	# get the highest matching JVM
769	for ((i = 0; i < ${#matchingJVMs[@]}; i++));
770	do
771		# split JVM string at ':' delimiter to retain spaces in $path substring
772		IFS=: arr=(${matchingJVMs[$i]}) ; unset IFS
773		# [0] comparable JVM version number
774		comp=${arr[0]}
775		# [1] JVM version number
776		ver=${arr[1]}
777		# [2] JVM path
778		path="${arr[2]}"
779
780		# use current value as JAVACMD if it's executable
781		if [ -x "$path" ] ; then
782			JAVACMD="$path"
783			JAVACMD_version=$comp
784			break
785		fi
786	done
787	# unset for loop variables
788	unset arr comp ver path
789fi
790
791# log the Java Command and the extracted version number
792stub_logger "[JavaCommand] '$JAVACMD'"
793stub_logger "[JavaVersion] $(get_java_version_from_cmd "${JAVACMD}")${JAVACMD_version:+ / $JAVACMD_version}"
794
795
796
797if [ -z "${JAVACMD}" ] || [ ! -x "${JAVACMD}" ] ; then
798
799	# different error messages when a specific JVM was required
800	if [ ! -z "${JVMVersion}" ] ; then
801		# display human readable java version (#28)
802		java_version_hr=$(echo ${JVMVersion} | sed -E 's/^1\.([0-9+*]+)$/ \1/g' | sed "s/+/ ${MSG_JAVA_VERSION_OR_LATER}/;s/*/ ${MSG_JAVA_VERSION_LATEST}/")
803		MSG_NO_SUITABLE_JAVA_EXPANDED=$(printf "${MSG_NO_SUITABLE_JAVA}" "${java_version_hr}").
804
805		if [ ! -z "${JVMMaxVersion}" ] ; then
806			java_version_hr=$(extract_java_major_version ${JVMVersion})
807			java_version_max_hr=$(echo ${JVMMaxVersion} | sed -E 's/^1\.([0-9+*]+)$/ \1/g' | sed "s/+//;s/*/ ${MSG_JAVA_VERSION_LATEST}/")
808			MSG_NO_SUITABLE_JAVA_EXPANDED="$(printf "${MSG_NO_SUITABLE_JAVA}" "${java_version_hr}") $(printf "${MSG_JAVA_VERSION_MAX}" "${java_version_max_hr}")"
809		fi
810
811		# log exit cause
812		stub_logger "[EXIT 3] ${MSG_NO_SUITABLE_JAVA_EXPANDED}"
813
814		# display error message with AppleScript
815		osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_NO_SUITABLE_JAVA_EXPANDED}\n${MSG_NO_SUITABLE_JAVA_CHECK}\" with title \"${CFBundleName}\"  buttons {\" OK \", \"${MSG_VISIT_JAVA_DOT_COM}\", \"${MSG_VISIT_ADOPTOPENJDK}\"} default button 1${DialogWithIcon}" \
816				-e "set response to button returned of the result" \
817				-e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" then open location \"https://www.java.com/download/\"" \
818				-e "if response is \"${MSG_VISIT_ADOPTOPENJDK}\" then open location \"https://adoptopenjdk.net/releases.html\""
819		# exit with error
820		exit 3
821
822	else
823		# log exit cause
824		stub_logger "[EXIT 1] ${MSG_ERROR_LAUNCHING}"
825		# display error message with AppleScript
826		osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_INSTALL_JAVA}\" with title \"${CFBundleName}\" buttons {\"${MSG_LATER}\", \"${MSG_VISIT_JAVA_DOT_COM}\", \"${MSG_VISIT_ADOPTOPENJDK}\"} default button 1${DialogWithIcon}" \
827					-e "set response to button returned of the result" \
828					-e "if response is \"${MSG_VISIT_JAVA_DOT_COM}\" then open location \"https://www.java.com/download/\"" \
829					-e "if response is \"${MSG_VISIT_ADOPTOPENJDK}\" then open location \"https://adoptopenjdk.net/releases.html\""
830		# exit with error
831		exit 1
832	fi
833fi
834
835
836
837# MainClass check
838############################################
839
840if [ -z "${JVMMainClass}" ]; then
841	# log exit cause
842	stub_logger "[EXIT 2] ${MSG_MISSING_MAINCLASS}"
843	# display error message with AppleScript
844	osascript -e "tell application \"System Events\" to display dialog \"${MSG_ERROR_LAUNCHING}\n\n${MSG_MISSING_MAINCLASS}\" with title \"${CFBundleName}\" buttons {\" OK \"} default button 1${DialogWithIcon}"
845	# exit with error
846	exit 2
847fi
848
849
850
851# execute $JAVACMD and do some preparation
852############################################
853
854# enable drag&drop to the dock icon
855export CFProcessPath="$0"
856
857# remove Apples ProcessSerialNumber from passthru arguments (#39)
858if [[ "$*" == -psn* ]] ; then
859	ArgsPassthru=()
860else
861	ArgsPassthru=("$@")
862fi
863
864# change to Working Directory based upon Apple/Oracle Plist info
865cd "${WorkingDirectory}" || exit 13
866stub_logger "[WorkingDirectory] ${WorkingDirectory}"
867
868# execute Java and set
869# - classpath
870# - splash image
871# - dock icon
872# - app name
873# - JVM options / properties (-D)
874# - JVM default options (-X)
875# - main class
876# - main class arguments
877# - passthrough arguments from Terminal or Drag'n'Drop to Finder icon
878stub_logger "[Exec] \"$JAVACMD\" -cp \"${JVMClassPath}\" ${JVMSplashFile:+ -splash:\"${ResourcesFolder}/${JVMSplashFile}\"} -Xdock:icon=\"${ResourcesFolder}/${CFBundleIconFile}\" -Xdock:name=\"${CFBundleName}\" ${JVMOptionsArr:+$(printf "'%s' " "${JVMOptionsArr[@]}") }${JVMDefaultOptions:+$JVMDefaultOptions }${JVMMainClass}${MainArgsArr:+ $(printf "'%s' " "${MainArgsArr[@]}")}${ArgsPassthru:+ $(printf "'%s' " "${ArgsPassthru[@]}")}"
879exec "${JAVACMD}" \
880		-cp "${JVMClassPath}" \
881		${JVMSplashFile:+ -splash:"${ResourcesFolder}/${JVMSplashFile}"} \
882		-Xdock:icon="${ResourcesFolder}/${CFBundleIconFile}" \
883		-Xdock:name="${CFBundleName}" \
884		${JVMOptionsArr:+"${JVMOptionsArr[@]}" }\
885		${JVMDefaultOptions:+$JVMDefaultOptions }\
886		"${JVMMainClass}"\
887		${MainArgsArr:+ "${MainArgsArr[@]}"}\
888		${ArgsPassthru:+ "${ArgsPassthru[@]}"}
889