Often as a developer you need to have multiple versions of Java installed on your mac. Some projects that you will work on may use an older version of Java (for e.g, 1.6 or 1.7), whilst I also like to dabble with the latest and greatest version to see what features are looming on the horizon.
As I use OS X extensively, I have traditionally fulfilled this requirement by using Virtual Machines either through Parallels or through VirtualBox. 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 OS X environment, and I don’t really like the context switching that occurs when using a VM.
Recently, I came across Homebrew Cask and 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 OS X and they assume you already have git and Homebrew installed.
From my experience, this is the safest, most ‘canonical’ way of installing and managing multiple versions of Java on OS X.
First, you need to install Homebrew Cask from brew:
brew install caskroom/cask/brew-cask
After Hombrew Cask is installed, let’s install Java from brew:
brew update && brew cask install java
Note: The above command will download the latest version of Java directly from Oracle.
Homebrew will take care of all the magic of registering plists (property list), and creating all the necessary links. Better yet, when you uninstall Java via homebrew, every bit of Java is cleanly removed including components that run during the startup.
Now you have the latest version of Java. But that still does not help you to manage multiple versions lying around on your mac. What if you want to use a mix of Oracle’s JDK with OpenJDK?
The most convenient way to install jEnv is via Homebrew:
brew install jenv
This should get you the latest stable version of jEnv.
Remember to run the following command to activate jEnv automatically on your terminal:
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(jenv init -)"' >> ~/.bash_profile
Note: I’m using Oh-My-Zsh if you’re using the regular bash replace ~/.bash_profile with ~/.profile
Now keep your shit clean:
brew update && brew cleanup && brew doctor
Note: For any newbies the above command will update Hombrew packages, it will clean any old packages and it will output any errors. If you get any warnings you should fix them before moving on.
Now that you have installed Java using both Homebrew and Homebrew Cask, let’s get jEnv to manage your prefer Java version. In OS X, all known JVM’s are located at /Library/Java/JavaVirtualMachines/. This is how to add Java and JDKs to jEnv:
jenv add /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home
Note: The above Java and JDK versions are the ones installed on my mac, they may differ on your machine. These are the latest versions as of March, 23, 2015 they may change overtime as new updates are release by Oracle.
Remember to run jEnv rehash to tell jEnv to look for all known binaries, and create the necessary shims:
List All Known Java Versions
To list all Java versions known to jEnv:
Output should be:
jenv versions system 1.7 22.214.171.124 1.8 126.96.36.199 oracle64-188.8.131.52 * oracle64-184.108.40.206 (set by /Users/Home/.jenv/version)
Note: Remember that you have to manually add new versions of Java to jEnv, and remove it from the list if a Java version is uninstalled.
Set Default Java Version
From the output above, you can see that I have Java 8 as the default version. Changing the default version of Java is as simple as:
jenv global oracle64-220.127.116.11
Let’s verify that indeed Java 8 is the default version:
Output should be:
java version "1.8.0_40" Java(TM) SE Runtime Environment (build 1.8.0_40-b25) Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
Change Java Version for A Project
Let’s say you have a project that you want to use Java 7. Instead of twiddling with JAVA_HOME, CLASSPATH and softlinks, you can tell jEnv to switch:
jenv local oracle64-18.104.22.168
jEnv will create a .java-version file in the current directory, and make sure the correct java version is linked.
Where is JAVA_HOME?
Traditionally we set JAVA_HOME to the home directory of the JDK version that we want. Since jEnv manages the links for us, JAVA_HOME is not automatically set:
You can set it by using jenv exec bash:
jenv exec bash bash-3.2$ echo $JAVA_HOME /Users/Home/.jenv/versions/oracle64-22.214.171.124 bash-3.2$
This works perfectly for startup scripts used by Tomcat, sbt and others.
There are a lot of other features in jEnv that is handy, including managing Java versions for other build tools such as sbt, maven and gradle. Spend a little bit of time going through the official documentation and increase your productivity using Java on OS X. Cheers.