Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@trmaphi
Last active April 5, 2024 04:06
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save trmaphi/3bc5b5a397a9491a61787143b098da85 to your computer and use it in GitHub Desktop.
Save trmaphi/3bc5b5a397a9491a61787143b098da85 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
@valtoni
Copy link

valtoni commented Aug 17, 2019

Nice!

@trmaphi
Copy link
Author

trmaphi commented Aug 19, 2019

Nice!

@valtoni, check this PR of my to extend this script https://github.com/Bash-it/bash-it/pull/1371/files

@valtoni
Copy link

valtoni commented Aug 26, 2019

@trmaphi thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment