good robots do what they're told

Easily Switch JDK on Mac

I have several versions of Java installed on my Mac. Trouble is, I can never remember where any of them are. So switching Java versions using the JAVA_HOME environment variable was always a pain. Then I discovered the handy java_home command.

/usr/libexec/java_home -V

It shows the Java versions are available and where there are. For example, on my machine, the output looks like this.

Matching Java Virtual Machines (4):
    1.8.0, x86_64:  "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
    1.7.0_25, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home
    1.6.0_65-b14-466.1, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    1.6.0_65-b14-466.1, i386:   "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

If you use the alternative option -v <version>, you get the path to a specific version.

/usr/libexec/java_home -v 1.8

It shows my 1.8 version lives at /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home.

Switch it!

To switch between versions, I can just run the following.

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

You could also add an alias to switch to a specific version;

alias setjava8='export JAVA_HOME=`/usr/libexec/java_home -v 1.8`'

If you want to do something more sophisticated, you could try something like this or this.

Caveat: I don’t have Java on my path, if you do, switching versions using the environment variable may not work.

Over to you...