Skip to content

Instantly share code, notes, and snippets.

@lsloan
Last active February 4, 2020 15:39
Show Gist options
  • Save lsloan/1a94bf3cb6178cc49d5c to your computer and use it in GitHub Desktop.
Save lsloan/1a94bf3cb6178cc49d5c to your computer and use it in GitHub Desktop.
setjdk() function for bash: Use "setjdk 1.7", "setjdk 1.8", or "setjdk" (to see current setting).
# Adapted from http://superuser.com/a/568016/64151
# Add this function to .bash_profile, then use
# "setjdk 1.7", "setjdk 1.8", or "setjdk" (to see current setting).
function setjdk() {
if [ $# -ne 0 ]; then
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
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
fi
echo JAVA_HOME set to $JAVA_HOME
java -version
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment