Run /usr/libexec/java_home -V
. This will show you all installed (and known) JDK's:
Matching Java Virtual Machines (3):
1.8.0_05, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
1.6.0_65-b14-462, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
Pick the version you want to be the default. In this example 1.6.0_65-b14-462
:
export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`
If you run java - version
now, you will see the changed version string.
It is also possible to specify just the major version, like:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Add the export JAVA_HOME ...
line to your shell’s init file.
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)
Just add the following line to the file:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0)
This gist is based on this stackoverflow question. It is basically the same content, but this way I don't need to seach for it every time again.