Skip to content

Instantly share code, notes, and snippets.

@dbuschman7
Last active May 23, 2018 16:22
Show Gist options
  • Save dbuschman7/6688de9c00371766599746d747a11f89 to your computer and use it in GitHub Desktop.
Save dbuschman7/6688de9c00371766599746d747a11f89 to your computer and use it in GitHub Desktop.
MacOS version handling
function removeFromPath() {
export PATH=$(echo ${PATH} | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
function showJdk() {
/usr/libexec/java_home
}
function setJdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
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}
echo ""
java -version
echo ""
fi
}
function listJdks() {
/usr/libexec/java_home -V
}
if [ -f /usr/libexec/java_home ]; then
export HISTORY=10000
echo ""
echo "Settings JDK ..."
setJdk 1.8
else
echo ""
echo -n "Configured Jdk is "
java -version
javac -version
echo ""
fi
$ listJdks
Matching Java Virtual Machines (3):
1.8.0_151, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
1.8.0_77, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home
1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
$ showJdk
/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
$ setJdk 1.7
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
$ setJdk 1.8
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment