If you’re a Java developer like me, working on a bunch of different projects at the same time, then you quite likely find yourself in the situation where you need to switch in between different JDK versions every time you switch context/project. In my case, I am working on the (good) stuff we got going in Cognitive Match which is mainly JDK 1.6 (yes, yes, I know, I know, don’t worry, we’re upgrading that soon). Then I also have a bunch of open source projects I work on (read “I stare at the code in github and don’t understand much” 🙂 ) which are using JDK 1.7. And also I’ve been playing with the new and shiny Java 8 too (loving the lambda expressions!).
Of course, I could take the approach of using just Java 8 and set compatibility mode (in maven or whatever) for the correct JDK version needed, however, this often highlights or hides problems specific with the JDK version used (when dealing with production projects especially I think it’s essential that developers mimic the production environment to the detail). As such, I prefer instead to have all the 3 versions installed on my Mac and switch in between them as and when I need it.
It turns out though that doing this can be a bit of a pain in the neck and requires a bit of a setup. So to save all of you in the same situation some time, I decided to post my solution for doing so online.
First and foremost, while it shouldn’t make any difference, it’s worth mentioning that I use Mac OS X 10.9.3 (Maverick). I use maven for a lot of the command line builds so what I’m going to present here refers to command line switching — if you use Eclipse, things are much easier, as Eclipse allows you to define upfront all of your JDK’s in the configuration section then you can assign a JDK / JRE per project. However, in the command line, things get trickier.
In my case, here are the 3 JDK versions and the paths they get installed in:
- JDK 1.6 : in
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
- JDK 1.7 : in
/Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home
- JDK 1.8 : in
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
To switch in between them, I simply have a bunch of aliases in my /etc/bashrc
which allow me to change the JAVA_HOME
environment variable. It turns out that doing so is simply enough:
alias jdk6="export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home ; java -version" alias jdk7="export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home ; java -version" alias jdk8="export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home ; java -version" |
Then when I need to switch in between any of them I simply use the above aliases and type: jdk6
or jdk7
or jdk8
depending on which java version I need to use. You can of course remove the java -version
call in the alias, but I find it less confusing that I get a clear visual indication as to what version I’m using once I’ve made the switch.