- # read JDK's from '/usr/libexec/java_home -V' command
- case `sw_vers -productVersion` in
- 10*)
- # read JDK's from '/usr/libexec/java_home -V' command
- while read -r line; do
- version=$(echo $line | awk -F $',' '{print $1;}')
- path=$(echo $line | awk -F $'" ' '{print $2;}')
- path+="/bin/java"
- allJVMs+=("$version:$path")
- done < <(/usr/libexec/java_home -V 2>&1 | grep '^[[:space:]]')
- ;;
- *)
- # read JDK's from java_home -X command
- while read version path; do
- path+="/bin/java"
- allJVMs+=("$version:$path")
- done < <(/usr/libexec/java_home -X |
- awk '/<dict>/ { version = ""; path = ""; }
- /<key>/ { key=$1; sub(/<key>/, "", key); sub(/<\/key>/, "", key); }
- /<string>/ { string=$1; sub(/<string>/, "", string); sub(/<\/string>/, "", string);
- if (key == "JVMHomePath") path = string;
- if (key == "JVMVersion") version = string;
- }
- /<\/dict>/ { if (version != "" && path != "") printf("%s %s\n", version, path); }')
- ;;
- esac
-
- # unset while loop variables
- unset version path
+
+ # read JDK's from '/usr/libexec/java_home --xml' command with PlistBuddy and a custom Dict iterator
+ # idea: https://stackoverflow.com/a/14085460/1128689 and https://scriptingosx.com/2018/07/parsing-dscl-output-in-scripts/
+ javaXml=$(/usr/libexec/java_home --xml)
+ javaCounter=$(/usr/libexec/PlistBuddy -c "Print" /dev/stdin <<< $javaXml | grep "Dict" | wc -l | tr -d ' ')
+
+ # iterate over all Dict entries
+ # but only if there are any JVMs at all (#93)
+ if [ "$javaCounter" -gt "0" ] ; then
+ for idx in $(seq 0 $((javaCounter - 1)))
+ do
+ version=$(/usr/libexec/PlistBuddy -c "print :$idx:JVMVersion" /dev/stdin <<< $javaXml)
+ path=$(/usr/libexec/PlistBuddy -c "print :$idx:JVMHomePath" /dev/stdin <<< $javaXml)
+ path+="/bin/java"
+ allJVMs+=("$version:$path")
+ done
+ # unset for loop variables
+ unset version path
+ fi
+
+ # add SDKMAN! java versions (#95)
+ if [ -d ~/.sdkman/candidates/java/ ] ; then
+ for sdkjdk in ~/.sdkman/candidates/java/*/
+ do
+ if [[ ${sdkjdk} =~ /current/$ ]] ; then
+ continue
+ fi
+
+ sdkjdkcmd="${sdkjdk}bin/java"
+ version=$(get_java_version_from_cmd "${sdkjdkcmd}")
+ allJVMs+=("$version:$sdkjdkcmd")
+ done
+ # unset for loop variables
+ unset version
+ fi