This page looks best with JavaScript enabled

Install Or Manage multiple versions of Java on OS X

 ·  ☕ 3 min read  ·  ✍️ Dinesh

A few weeks ago my Mac hard drive crashed and I had to get a new grad drive. As part of upgrade, I had to wipe my drive clean and install Yosemite. What I did not realize was that Apple had goofed up Java instlation on Mac, as result of which my IntelliJ idea compalained that it requires legacy Jdk version of Java 6. The dilemma that I had was hwo am I going to mainain the different version of Java.

HomeBrew as always came to my rescue. For those who are not sure what is HomeBrew and what it is used for should definitely install brew. With so many apps and softwares out there, one needs to maintain several versions of Java JDK as not all of them are compatible with all the versions of Java. The problem with that approach is that you may have downloaded all the versions of Java JDK but you have to tweak around the the bash profiles, set environment, JAVA_HOME settings etc., which is kind of a pain and error prone. This is where neat little tool called jEnv comes into picture. This tool allows you to change different java versions using a simple command. It will be familair to anyone who has used RVM. To read more about, see this post.

So Let’s begin by installing jEnv

Step 1: Run this in the terminal

brew install https://raw.github.com/gcuisinier/jenv/homebrew/jenv.rb

Step 2: Add jEnv to the bash profile

if which jenv > /dev/null; then eval "$(jenv init -)"; fi

Step 3: When you first install jEnv will not have any JDK associated with it.

For example, I just installed JDK 8 but jEnv does not know about it. To check Java versions on jEnv

At the moment it only found Java version(jre) on the system. The ‘*’ shows the version currently selected. Unline rvm and rbenv, jEnv cannot install JDK for you. You need to install JDK manually from Oracle website.

Step 4: Install JDK 6 from Apple website. This will install Java /System/Library/Java/JavaVirtualMachines/ . The reason we are installing Java 6 from Apple website is that SUN did not come up with JDK 6 for MAC, so Apple created/modified its own deployment version.

Step 5: Similarly install JDK7 and JDK8.

Step 6: Add JDKs to jEnv.

JDK 6:

JDK 7:

JDK 8:

Step 8: Check the java versions installed using jenv

Step 9: So now we have 3 versions of Java on our system. To set a default version use the command

jenv local <jenv version>

Ex – I wanted Jdk 1.6 to start IntelliJ

jenv local oracle64-1.6.0.65

Step 10: check the java version

java -version

That’s it. We now have multiple versions of java and we can switch between them easily. jEnv also has some other features, such as wrappers for Gradle, Ant, Maven, etc, and the ability to set JVM options globally or locally. Check out the documentation for more information.

~~Ciao

Share on
Support the author with

Dinesh Arora
WRITTEN BY
Dinesh
Developer