33# #
44# universalJavaApplicationStub #
55# #
6- # #
76# A shellscript JavaApplicationStub for Java Apps on Mac OS X #
87# that works with both Apple's and Oracle's plist format. #
98# #
109# Inspired by Ian Roberts stackoverflow answer #
1110# at http://stackoverflow.com/a/17546508/1128689 #
1211# #
13- # #
1412# @author Tobias Fischer #
1513# @url https://github.com/tofi86/universalJavaApplicationStub #
1614# @date 2017-10-30 #
1715# @version 2.1.0 #
1816# #
19- # #
2017# #################################################################################
2118# #
22- # #
2319# The MIT License (MIT) #
2420# #
2521# Copyright (c) 2017 Tobias Fischer #
4642
4743
4844
49- # helper function:
50- # A logger which logs to the macOS Console.app
51- # ###############################################
52- function stubLogger() {
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() {
5353 syslog -s -k \
5454 Facility com.apple.console \
5555 Level Notice \
@@ -59,7 +59,6 @@ function stubLogger() {
5959
6060
6161
62- #
6362# set the directory abspath of the current
6463# shell script with symlinks being resolved
6564# ###########################################
@@ -75,11 +74,10 @@ while [ -h "$PRG" ]; do
7574 fi
7675done
7776PROGDIR=` dirname " $PRG " `
78- stubLogger " [StubDir] $PROGDIR "
77+ stub_logger " [StubDir] $PROGDIR "
7978
8079
8180
82- #
8381# set files and folders
8482# ###########################################
8583
@@ -111,7 +109,6 @@ JVMVersion=""
111109
112110
113111
114- #
115112# read Info.plist and extract JVM options
116113# ###########################################
117114
137134
138135# read Info.plist in Apple style if exit code returns 0 (true, :Java key is present)
139136if [ $exitcode -eq 0 ]; then
140- stubLogger " [PlistStyle] Apple"
137+ stub_logger " [PlistStyle] Apple"
141138
142139 # set Java and Resources folder
143140 JavaFolder=" ${AppleJavaFolder} "
@@ -206,9 +203,10 @@ if [ $exitcode -eq 0 ]; then
206203 # read the Java version we want to find
207204 JVMVersion=` /usr/libexec/PlistBuddy -c " print ${JavaKey} :JVMVersion" " ${InfoPlistFile} " 2> /dev/null | xargs`
208205
206+
209207# read Info.plist in Oracle style
210208else
211- stubLogger " [PlistStyle] Oracle"
209+ stub_logger " [PlistStyle] Oracle"
212210
213211 # set Working Directory and Java and Resources folder
214212 JavaFolder=" ${OracleJavaFolder} "
257255
258256
259257
260- #
261258# internationalized messages
262259# ###########################################
263260
264261LANG=` defaults read -g AppleLocale`
265- stubLogger " [Language] $LANG "
262+ stub_logger " [Language] $LANG "
266263
267264# French localization
268265if [[ $LANG == fr* ]] ; then
303300
304301
305302
306- # helper function:
307- # extract Java version string from 'java -version' command
303+ # function 'get_java_version_from_cmd()'
304+ #
305+ # returns Java version string from 'java -version' command
308306# works for both old (1.8) and new (9) version schema
309- # #########################################################
310- function extractJavaVersionString() {
307+ #
308+ # @param1 path to a java JVM executable
309+ # @return the Java version number as displayed in 'java -version' command
310+ # ###############################################################################
311+ function get_java_version_from_cmd() {
311312 # second sed command strips " and -ea from the version string
312313 echo $( " $1 " -version 2>&1 | awk ' /version/{print $NF}' | sed -E ' s/"//g;s/-ea//g' )
313314}
314315
315316
316- # helper function:
317- # extract Java major version from a Java version string
318- # #########################################################
319- function extractJavaMajorVersion() {
317+ # function 'extract_java_major_version()'
318+ #
319+ # extract Java major version from a version string
320+ #
321+ # @param1 a Java version number ('1.8.0_45') or requirement string ('1.8+')
322+ # @return the major version (e.g. '7', '8' or '9', etc.)
323+ # ###############################################################################
324+ function extract_java_major_version() {
320325 echo $( echo " $1 " | sed -E ' s/^1\.//;s/^([0-9]+)(-ea|(\.[0-9_.]{1,7})?)[+*]?$/\1/' )
321326}
322327
323328
324- # helper function:
325- # return comparable version for java version string
326- # return value is an 8 digit numeral
327- # #########################################################
328- function comparableJavaVersionNumber() {
329+ # function 'get_comparable_java_version()'
330+ #
331+ # return comparable version for a Java version number or requirement string
332+ #
333+ # @param1 a Java version number ('1.8.0_45') or requirement string ('1.8+')
334+ # @return an 8 digit numeral ('1.8.0_45'->'08000045'; '9.1.13'->'09001013')
335+ # ###############################################################################
336+ function get_comparable_java_version() {
329337 # cleaning: 1) remove leading '1.'; 2) remove 'a-Z' and '-*+' (e.g. '-ea'); 3) replace '_' with '.'
330338 cleaned=$( echo " $1 " | sed -E ' s/^1\.//g;s/[a-zA-Z+*\-]//g;s/_/./g' )
331339 # splitting at '.' into an array
@@ -335,14 +343,16 @@ function comparableJavaVersionNumber() {
335343}
336344
337345
338- # function:
339- # Java version tester checks whether a given java version
346+ # function 'does_java_version_satisfy_requirement()'
347+ #
348+ # this function checks whether a given java version number
340349# satisfies the given requirement
341- # - parameter1: the java major version (6, 7, 8, 9, etc.)
342- # - parameter2: the java requirement (1.6, 1.7+, etc.)
343- # - return: 0 (satiesfies), 1 (does not), 2 (error)
344- # #########################################################
345- function JavaVersionSatisfiesRequirement() {
350+ #
351+ # @param1 the java major version (6, 7, 8, 9, etc.)
352+ # @param2 the java requirement (1.6, 1.7+, etc.)
353+ # @return an exit code: 0 (satiesfies), 1 (does not), 2 (error)
354+ # ###############################################################################
355+ function does_java_version_satisfy_requirement() {
346356 java_ver=$1
347357 java_req=$2
348358
@@ -360,8 +370,8 @@ function JavaVersionSatisfiesRequirement() {
360370 # matches requirements with + modifier
361371 # e.g. 1.8+, 9+, 9.1+, 9.2.4+, 10+, 10.1+, 10.1.35+
362372 elif [[ ${java_req} =~ ^[0-9]+ (\. [0-9]+)* \+ $ ]] ; then
363- java_req_num=$( comparableJavaVersionNumber ${java_req} )
364- java_ver_num=$( comparableJavaVersionNumber ${java_ver} )
373+ java_req_num=$( get_comparable_java_version ${java_req} )
374+ java_ver_num=$( get_comparable_java_version ${java_ver} )
365375 if [ ${java_ver_num} -ge ${java_req_num} ] ; then
366376 return 0
367377 else
@@ -386,18 +396,17 @@ function JavaVersionSatisfiesRequirement() {
386396
387397
388398
389- #
390399# find installed Java versions
391400# ###########################################
392401
393402apple_jre_plugin=" /Library/Java/Home/bin/java"
394- apple_jre_version=$( extractJavaMajorVersion $( extractJavaVersionString " ${apple_jre_plugin} " ) )
403+ apple_jre_version=$( extract_java_major_version $( get_java_version_from_cmd " ${apple_jre_plugin} " ) )
395404oracle_jre_plugin=" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"
396- oracle_jre_version=$( extractJavaMajorVersion $( extractJavaVersionString " ${oracle_jre_plugin} " ) )
405+ oracle_jre_version=$( extract_java_major_version $( get_java_version_from_cmd " ${oracle_jre_plugin} " ) )
397406
398407# first check system variable "$JAVA_HOME"
399408if [ -n " $JAVA_HOME " ] ; then
400- stubLogger ' [JavaSearch] Checking $JAVA_HOME'
409+ stub_logger ' [JavaSearch] Checking $JAVA_HOME'
401410
402411 # PR 26: Allow specifying "$JAVA_HOME" relative to "$AppPackageFolder"
403412 # which allows for bundling a custom version of Java inside your app!
@@ -411,17 +420,17 @@ if [ -n "$JAVA_HOME" ] ; then
411420
412421# check for a specific Java version, specified in JVMversion Plist key
413422elif [ ! -z ${JVMVersion} ] ; then
414- stubLogger " [JavaSearch] Checking for specific Java version '${JVMVersion} '"
423+ stub_logger " [JavaSearch] Checking for specific Java version '${JVMVersion} '"
415424
416425 # first check "/usr/libexec/java_home" symlinks
417426 if [ -x /usr/libexec/java_home ] && /usr/libexec/java_home -F -v ${JVMVersion} > /dev/null 2>&1 ; then
418427 JAVACMD=" ` /usr/libexec/java_home -F -v ${JVMVersion} 2> /dev/null` /bin/java"
419- JAVACMD_version=$( comparableJavaVersionNumber $( extractJavaVersionString " ${JAVACMD} " ) )
428+ JAVACMD_version=$( get_comparable_java_version $( get_java_version_from_cmd " ${JAVACMD} " ) )
420429 fi
421430
422431 # then additionally check the Oracle JRE plugin whether it's a higher/newer compatible version
423- if [ -x " ${oracle_jre_plugin} " ] && JavaVersionSatisfiesRequirement ${oracle_jre_version} ${JVMVersion} ; then
424- this_java_ver=$( comparableJavaVersionNumber $( extractJavaVersionString " ${oracle_jre_plugin} " ) )
432+ if [ -x " ${oracle_jre_plugin} " ] && does_java_version_satisfy_requirement ${oracle_jre_version} ${JVMVersion} ; then
433+ this_java_ver=$( get_comparable_java_version $( get_java_version_from_cmd " ${oracle_jre_plugin} " ) )
425434 # use this compatible version only if the above returned empty or if the version number is higher
426435 if [ -z " ${JAVACMD} " ] || [ ${this_java_ver} -ge ${JAVACMD_version} ] ; then
427436 JAVACMD=" ${oracle_jre_plugin} "
@@ -430,8 +439,8 @@ elif [ ! -z ${JVMVersion} ] ; then
430439 fi
431440
432441 # then additionally check the Apple JRE plugin whether it's a higher/newer compatible version
433- if [ -x " ${apple_jre_plugin} " ] && JavaVersionSatisfiesRequirement ${apple_jre_version} ${JVMVersion} ; then
434- this_java_ver=$( comparableJavaVersionNumber $( extractJavaVersionString " ${apple_jre_plugin} " ) )
442+ if [ -x " ${apple_jre_plugin} " ] && does_java_version_satisfy_requirement ${apple_jre_version} ${JVMVersion} ; then
443+ this_java_ver=$( get_comparable_java_version $( get_java_version_from_cmd " ${apple_jre_plugin} " ) )
435444 # use this compatible version only if the above returned empty or if the version number is higher
436445 if [ -z " ${JAVACMD} " ] || [ ${this_java_ver} -ge ${JAVACMD_version} ] ; then
437446 JAVACMD=" ${apple_jre_plugin} "
@@ -444,7 +453,7 @@ elif [ ! -z ${JVMVersion} ] ; then
444453 java_version_hr=$( echo ${JVMVersion} | sed -E ' s/^1\.([0-9+*]+)$/ \1/g' | sed " s/+/ ${MSG_JAVA_VERSION_OR_LATER} /" | sed " s/*/ ${MSG_JAVA_VERSION_LATEST} /" )
445454 MSG_NO_SUITABLE_JAVA_EXPANDED=$( printf " ${MSG_NO_SUITABLE_JAVA} " " ${java_version_hr} " )
446455 # log exit cause
447- stubLogger " [EXIT 3] ${MSG_NO_SUITABLE_JAVA_EXPANDED} "
456+ stub_logger " [EXIT 3] ${MSG_NO_SUITABLE_JAVA_EXPANDED} "
448457 # display error message with AppleScript
449458 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)" \
450459 -e " set response to button returned of the result" \
@@ -455,17 +464,17 @@ elif [ ! -z ${JVMVersion} ] ; then
455464
456465# otherwise check "/usr/libexec/java_home" and Oracle and Apple JRE paths and use highest version available
457466else
458- stubLogger " [JavaSearch] Checking for other Java versions"
467+ stub_logger " [JavaSearch] Checking for other Java versions"
459468
460469 # first check "/usr/libexec/java_home" symlinks
461470 if [ -x /usr/libexec/java_home ] && /usr/libexec/java_home -F > /dev/null 2>&1 ; then
462471 JAVACMD=" ` /usr/libexec/java_home 2> /dev/null` /bin/java"
463- JAVACMD_version=$( comparableJavaVersionNumber $( extractJavaVersionString " ${JAVACMD} " ) )
472+ JAVACMD_version=$( get_comparable_java_version $( get_java_version_from_cmd " ${JAVACMD} " ) )
464473 fi
465474
466475 # then additionally check the Oracle JRE plugin whether it's a higher/newer compatible version
467476 if [ -x " ${oracle_jre_plugin} " ] ; then
468- this_java_ver=$( comparableJavaVersionNumber $( extractJavaVersionString " ${oracle_jre_plugin} " ) )
477+ this_java_ver=$( get_comparable_java_version $( get_java_version_from_cmd " ${oracle_jre_plugin} " ) )
469478 # use this compatible version only if the above returned empty or if the version number is higher
470479 if [ -z " ${JAVACMD} " ] || [ ${this_java_ver} -ge ${JAVACMD_version} ] ; then
471480 JAVACMD=" ${oracle_jre_plugin} "
475484
476485 # then additionally check the Apple JRE plugin whether it's a higher/newer compatible version
477486 if [ -x " ${apple_jre_plugin} " ] ; then
478- this_java_ver=$( comparableJavaVersionNumber $( extractJavaVersionString " ${apple_jre_plugin} " ) )
487+ this_java_ver=$( get_comparable_java_version $( get_java_version_from_cmd " ${apple_jre_plugin} " ) )
479488 # use this compatible version only if the above returned empty or if the version number is higher
480489 if [ -z " ${JAVACMD} " ] || [ ${this_java_ver} -ge ${JAVACMD_version} ] ; then
481490 JAVACMD=" ${apple_jre_plugin} "
@@ -485,22 +494,21 @@ else
485494fi
486495
487496# log the Java Command and the extracted version number
488- stubLogger " [JavaCommand] $JAVACMD "
489- stubLogger " [JavaVersion] $( extractJavaVersionString " ${JAVACMD} " ) ${JAVACMD_version: + / $JAVACMD_version } "
497+ stub_logger " [JavaCommand] $JAVACMD "
498+ stub_logger " [JavaVersion] $( get_java_version_from_cmd " ${JAVACMD} " ) ${JAVACMD_version: + / $JAVACMD_version } "
490499
491500# fallback fallback: /usr/bin/java
492501# but this would prompt to install deprecated Apple Java 6
493502
494503
495504
496- #
497- # execute JAVA commandline and do some pre-checks
498- # ###################################################
505+ # execute $JAVACMD and do some pre-checks
506+ # ###########################################
499507
500508# display error message if MainClassName is empty
501509if [ -z ${JVMMainClass} ]; then
502510 # log exit cause
503- stubLogger " [EXIT 2] ${MSG_MISSING_MAINCLASS} "
511+ stub_logger " [EXIT 2] ${MSG_MISSING_MAINCLASS} "
504512 # display error message with AppleScript
505513 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)"
506514 # exit with error
@@ -522,7 +530,7 @@ elif [ -f "${JAVACMD}" ] && [ -x "${JAVACMD}" ] ; then
522530
523531 # change to Working Directory based upon Apple/Oracle Plist info
524532 cd " ${WorkingDirectory} "
525- stubLogger " [WorkingDirectory] ${WorkingDirectory} "
533+ stub_logger " [WorkingDirectory] ${WorkingDirectory} "
526534
527535 # execute Java and set
528536 # - classpath
@@ -534,7 +542,7 @@ elif [ -f "${JAVACMD}" ] && [ -x "${JAVACMD}" ] ; then
534542 # - main class
535543 # - main arguments
536544 # - passthru arguments
537- stubLogger " [Exec] \" $JAVACMD \" -cp \" ${JVMClassPath} \" -splash:\" ${ResourcesFolder} /${JVMSplashFile} \" -Xdock:icon=\" ${ResourcesFolder} /${CFBundleIconFile} \" -Xdock:name=\" ${CFBundleName} \" ${JVMOptions: +$JVMOptions }${JVMDefaultOptions: +$JVMDefaultOptions }${JVMMainClass}${MainArgs: + $MainArgs }${ArgsPassthru: + $ArgsPassthru } "
545+ stub_logger " [Exec] \" $JAVACMD \" -cp \" ${JVMClassPath} \" -splash:\" ${ResourcesFolder} /${JVMSplashFile} \" -Xdock:icon=\" ${ResourcesFolder} /${CFBundleIconFile} \" -Xdock:name=\" ${CFBundleName} \" ${JVMOptions: +$JVMOptions }${JVMDefaultOptions: +$JVMDefaultOptions }${JVMMainClass}${MainArgs: + $MainArgs }${ArgsPassthru: + $ArgsPassthru } "
538546 exec " ${JAVACMD} " \
539547 -cp " ${JVMClassPath} " \
540548 -splash:" ${ResourcesFolder} /${JVMSplashFile} " \
@@ -549,7 +557,7 @@ elif [ -f "${JAVACMD}" ] && [ -x "${JAVACMD}" ] ; then
549557
550558else
551559 # log exit cause
552- stubLogger " [EXIT 1] ${MSG_ERROR_LAUNCHING} "
560+ stub_logger " [EXIT 1] ${MSG_ERROR_LAUNCHING} "
553561 # display error message with AppleScript
554562 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)" \
555563 -e " set response to button returned of the result" \
0 commit comments