Skip to content

Instantly share code, notes, and snippets.

@marnovo
Forked from trmaphi/changeJDK.bash
Created October 20, 2020 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marnovo/e9e0e8989a3228d6c9ddda82768f934a to your computer and use it in GitHub Desktop.
Save marnovo/e9e0e8989a3228d6c9ddda82768f934a to your computer and use it in GitHub Desktop.
[Change system wide java version on Mac OS] Inspire by a stackoverflow answer https://stackoverflow.com/a/44169445/6730571 #bash #mac
#!/usr/bin/env bash
JDKS_DIR="/Library/Java/JavaVirtualMachines"
JDKS=( $(ls ${JDKS_DIR}) )
JDKS_STATES=()
# Map state of JDK
for (( i = 0; i < ${#JDKS[@]}; i++ )); do
if [[ -f "${JDKS_DIR}/${JDKS[$i]}/Contents/Info.plist" ]]; then
JDKS_STATES[${i}]=enable
else
JDKS_STATES[${i}]=disable
fi
echo "${i} ${JDKS[$i]} ${JDKS_STATES[$i]}"
done
# Declare variables
DEFAULT_JDK_DIR=""
DEFAULT_JDK=""
OPTION=""
# OPTION for default jdk and set variables
while [[ ! "$OPTION" =~ ^[0-9]+$ || OPTION -ge "${#JDKS[@]}" ]]; do
read -p "Enter Default JDK: " OPTION
if [[ ! "$OPTION" =~ ^[0-9]+$ ]]; then
echo "Sorry integers only"
fi
if [[ OPTION -ge "${#JDKS[@]}" ]]; then
echo "Out of index"
fi
done
DEFAULT_JDK_DIR="${JDKS_DIR}/${JDKS[$OPTION]}"
DEFAULT_JDK="${JDKS[$OPTION]}"
# Disable all jdk
for (( i = 0; i < ${#JDKS[@]}; i++ )); do
if [[ -f "${JDKS_DIR}/${JDKS[$i]}/Contents/Info.plist" ]]; then
sudo mv "${JDKS_DIR}/${JDKS[$i]}/Contents/Info.plist" "${JDKS_DIR}/${JDKS[$i]}/Contents/Info.plist.disable"
fi
done
# Enable default jdk
if [[ -f "${DEFAULT_JDK_DIR}/Contents/Info.plist.disable" ]]; then
sudo mv "${DEFAULT_JDK_DIR}/Contents/Info.plist.disable" "${DEFAULT_JDK_DIR}/Contents/Info.plist"
echo "Enable ${DEFAULT_JDK} as default JDK"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment