I had a similar situation at work, so I did a bit of research and, with a couple minutes of setup, it can be almost painless to switch back and forth between Java 6 and 7 (or 7 and 8, for that matter - this method is version agnostic).
For anyone with java installed, /usr/libexec/java_home
will show the JAVA_HOME directory, but it's especially powerful when you have multiple versions of java installed side-by-side on OS X.
Using the -V
flag, you can see all of the installed JVMs:
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
1.7.0_67, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_67.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
You can also get the JAVA_HOME value for a specific JVM like this:
$ /usr/libexec/java_home -v 1.6
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
$ /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home
Putting this all together, this is what I added to my ~/.profile:
## java home
alias j6="export JAVA_HOME=\$(/usr/libexec/java_home -v 1.6);echo 'using Java 6'"
alias j7="export JAVA_HOME=\$(/usr/libexec/java_home -v 1.7);echo 'using Java 7'"
## manual switch for 1.6 or 1.7
j6 ## enable java 6 (or just run j6 in terminal)
## j7 ## enable java 7 (or just run j7 in terminal)
So you can see that right now, my terminal defaults to Java6, but I can switch this by just typing j7
on the command line (or editing the ~/.profile):
$ 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)
$ j7
using Java 7
$ java -version
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)