DevOps Zone is brought to you in partnership with:

I'm a coder from way back starting mostly with Java and moving on to cooler things like Rails. I like to dabble with newer technologies and languages. I do also run a fair bit to balance out the coding but am not very good at it as evidenced by my finishing times. Ricky is a DZone MVB and is not an employee of DZone and has posted 17 posts at DZone. You can read more from them at their website. View Full User Profile

Running Multiple JDKs on OSX

03.11.2014
| 4415 views |
  • submit to reddit

I often need to have multiple versions of Java installed on my machine. Some projects I work on use an older version of Java (for e.g, 1.6), whilst I also like to dabble with the latest and greatest version to see what features are looming on the horizon. As I use OSX extensively, I have traditionally fulfilled this requirement by using Virtual Machines either through vmware fusion or through virtual box. However, the biggest downfall with this approach is the performance of running VMs as they can be quite resource intensive. Also, I like working in a native OSX environment, and I don't really like the context switching that occurs when using a VM.

Recently, I came across jENV which I describe as rbenv or RVM for Java. Essentially it allows developers to switch between different versions of Java as required. So the rest of this article will take you through the very simple steps of how to set jENV up.

The following instructions are for OSX and they assume you have brewinstalled.

Firstly, you need to install jENV from brew.

Technically, you need git installed first, but I would imagine most people would have git already.

$ brew install git
$ brew tap jenv/jenv
$ brew install jenv

Next thing you will need to do is update your .bash_profile script, that is assuming you use bash.

$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile

Now if everything has worked properly, you should be able to start adding new JDKs and configuring the current Java version as shown below. Make sure the paths below point to JDKs that you have installed.

$ jenv add /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
$ jenv global oracle64-1.8.0-ea
$ jenv versions

  system
  oracle64-1.6.0.65
* oracle64-1.8.0-ea (set by /Users/ricky/.jenv/version)
$ java -version
java version "1.8.0-ea"  
Java(TM) SE Runtime Environment (build 1.8.0-ea-b108)  
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b50, mixed mode)

$ jenv global oracle64-1.6.0.65
$ java -version
java version "1.6.0_65"  
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)  
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)  

For more information, please check out the official site here.

Published at DZone with permission of Ricky Yim, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)