Mac OS X: Setting Default Java Version

Let’s assume we have installed JDK 7 or JDK 8, and we want to set the default Java version. There are several ways to set default Java version. This is by far the easiest way without the need to look for the installed path or mess around with symlinks.

First, let’s make sure we are not on crack and we indeed installed the version we want:-

➜  ~  /usr/libexec/java_home -verbose
Matching Java Virtual Machines (3):
    1.7.0_25, x86_64:	"Java SE 7"	/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home
    1.6.0_51-b11-457, x86_64:	"Java SE 6"	/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    1.6.0_51-b11-457, i386:	"Java SE 6"	/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

In this example, we are going to use JDK 1.7 as our default Java version. Open up the startup file (.bashrc, .zshrc, etc…) and enter this line:-

export JAVA_HOME="`/usr/libexec/java_home -v '1.7*'`"

Restart Terminal and verify the Java version:-

➜  ~  java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
➜  ~  javac -version
javac 1.7.0_25

Leave a Reply

Your email address will not be published.