Skip to content

Instantly share code, notes, and snippets.

@nantsou
Last active February 5, 2022 06:43
Show Gist options
  • Save nantsou/767874edf435fdbe0241f41d4b2091db to your computer and use it in GitHub Desktop.
Save nantsou/767874edf435fdbe0241f41d4b2091db to your computer and use it in GitHub Desktop.
bash function to set java version in mac
## remove a path from PATH
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
## set JAVA env with given version
function setJDK() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
unset JAVA_HOME
export JAVA_HOME=`/usr/libexec/java_home -v $@`
export PATH=$JAVA_HOME/bin:$PATH
java -version
javac -version
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment