Skip to content

Instantly share code, notes, and snippets.

@jeffjohnson9046
Created May 24, 2016 15:00
Show Gist options
  • Save jeffjohnson9046/eafc70ba7e8d9c056de80d9c6ef6098c to your computer and use it in GitHub Desktop.
Save jeffjohnson9046/eafc70ba7e8d9c056de80d9c6ef6098c to your computer and use it in GitHub Desktop.
A function for switching between Java JDKs on OS X
# I'm in a situation where I have multiple JDKs installed for various projects. While I'm sure there are a million was to handle this
# issue (a number of sites recommended jenv), I just wanted a quick & easy way to switch between JDKs that are installed on my machine.
# I took the following steps:
#
# 1. Add the functions below to your ~/.bash_profile
# 2. Source your ~/.bash_profile (command is . ~/.bash_profile) or exit and reload your terminal window
#
# These functions came from here: https://www.jayway.com/2014/01/15/how-to-switch-jdk-version-on-mac-os-x-maverick/, so all credit goes to the author of that article
function setjdk() {
if [ $# -ne 0 ]; then
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
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
setjdk 1.8 # Set the JDK on startup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment