From: https://wimdeblauwe.wordpress.com/2014/03/20/switching-easily-between-java-jdks-on-mac-os-x/
First, add the following aliases in ~/.profile file:
alias setJdk6='export JAVA_HOME=$(/usr/libexec/java_home -v 1.6)' alias setJdk7='export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)' alias setJdk8='export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)'
Open a new terminal window or use source ~/.profile to load the aliases.
Now you can easily switch between the installed JDKs:
> setJdk6
We can verify that Maven picks this up correctly:
> mvn --version Apache Maven 3.0.4 (r1232337; 2012-01-17 09:44:56+0100) Maven home: /Applications/maven/apache-maven-3.0.4 Java version: 1.6.0_65, vendor: Apple Inc. Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home Default locale: en_US, platform encoding: MacRoman OS name: "mac os x", version: "10.9.2", arch: "x86_64", family: "mac"