Skip to content

Instantly share code, notes, and snippets.

@timyates
Created August 14, 2014 07:47
Show Gist options
  • Save timyates/7c6c5bee0ff76bfc9ae0 to your computer and use it in GitHub Desktop.
Save timyates/7c6c5bee0ff76bfc9ae0 to your computer and use it in GitHub Desktop.
my latest jdk switcher for os x bash

Once this is in your shell, you can just do:

$ jdk 6
Using Java 6 /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

$ java -version
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)

$ jdk 7
Using Java 7 /Library/Java/JavaVirtualMachines/jdk1.7.0_65.jdk/Contents/Home

$ java -version
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

$ jdk 8
Using Java 8 /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home

$ java -version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)
function jdk {
export JAVA_HOME=`/usr/libexec/java_home -v 1.$1*`
set_path
echo "Using Java $1 $JAVA_HOME"
}
function set_path {
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin
PATH=$PATH:$JAVA_HOME/bin
# Set other path vars here
# Reload GVM
export GVM_INIT=
[[ -s "/Users/tim.yates/.gvm/bin/gvm-init.sh" && ! $(which gvm-init.sh) ]] && source "/Users/tim.yates/.gvm/bin/gvm-init.sh"
}
set_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment