Skip to content

Instantly share code, notes, and snippets.

@hanrw
Forked from donchesworth/.bash_profile
Last active March 17, 2022 03:49
Show Gist options
  • Save hanrw/c6423978484046a650b5a796f2e53330 to your computer and use it in GitHub Desktop.
Save hanrw/c6423978484046a650b5a796f2e53330 to your computer and use it in GitHub Desktop.
switch between java versions
# Simple way ============================
function setjdk() {
if [ $# -ne 0 ]; then
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath "$JAVA_HOME/bin"
fi
export JAVA_HOME=`/usr/libexec/java_home -v $@`
export PATH=$JAVA_HOME/bin:$PATH
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
# Simple way 2 ============================
function setjdk() {
if [ $# -ne 0 ]; then
switchjdk $@
else
echo "Choose from the following Java versions:"
choosejdk
echo "Please enter the correct full version number:"
read jdkPick
switchjdk $jdkPick
fi
}
function switchjdk() {
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=$(/usr/libexec/java_home -v $@)
export PATH=$JAVA_HOME/bin:$PATH
}
function choosejdk() {
allVersions=$(/usr/libexec/java_home -V)
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
# For zsh Function to switch JDK versions
function setjdk() {
echo "Select the java version you wish to switch to from the following:"
echo
allVersions=()
count=0
while IFS= read -r line; do
if [[ ! "$line" == M* ]] && [[ ! "$line" == /* ]] && [[ ! -z "$line" ]]; then
allVersions+=($line)
((count++))
echo '['"$count"']'${allVersions[$count]}
fi
done < <(/usr/libexec/java_home -V 2>&1)
echo "Please chose a value from the options above:"
read selectedOption </dev/tty
if [ "$count" -ge "$selectedOption" ] && [ "$selectedOption" -ge '0' ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
removeFromPath "$JAVA_HOME/bin"
stringArray=(${allVersions[$selectedOption]})
export JAVA_HOME="${stringArray[@]: -1}"
export PATH="$JAVA_HOME"/bin:$PATH
echo "JAVA_HOME is set to be ${JAVA_HOME}"
else
echo "Invalid option, JAVA_HOME was not set"
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
# For bash Function to switch JDK versions
function setjdk() {
echo "Select the java version you wish to switch to from the following:"
echo
options=()
count=-1
while IFS= read -r line
do
if [[ ! "$line" == M* ]] && [[ ! "$line" == /* ]] && [[ ! -z "$line" ]]; then
options+=("$line")
((count++))
echo '['"$count"']'${options["$count"]}
fi
done < <(/usr/libexec/java_home -V 2>&1)
echo
read -p "Please chose a value from the options above: " selectedOption </dev/tty
if [ "$count" -ge "$selectedOption" ] && [ "$selectedOption" -ge '0' ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
removeFromPath "$JAVA_HOME/bin"
stringArray=(${options["$selectedOption"]})
export JAVA_HOME="${stringArray[@]: -1}"
export PATH="$JAVA_HOME"/bin:$PATH
echo "JAVA_HOME is set to be ${JAVA_HOME}"
else
echo "Invalid option, JAVA_HOME was not set"
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
@hanrw
Copy link
Author

hanrw commented Apr 14, 2021

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