Install old JDK versions (1.6, 1.7) in Mac OS X Yosemite

published on on macOS, JDK

It's already been while since Apple removed the "Java Preference Pane" from OS X, with which it was possible to change between various installed Java versions. I shortly needed to compile some old Java application against the JDK version 1.6 and I was questioning how to change the compiler version. Here's how:

JDK 1.6:

export JAVA_HOME=`/usr/libexec/java_home -v 1.6`

JDK 1.7:

export JAVA_HOME=`/usr/libexec/java_home -v 1.7`

Then you can query the currently active version by issuing java -version or javac -version:

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)
javac -version
javac 1.6.0_65

If you want those changed to be persistent, don't forget to add the export statement to your .bashrc, .profile, .zshrc files...