Simplicity is a Virtue

I recently received a new MacBook Pro and set out on the usual tasks of getting all my preferred development tools and frameworks installed. As a Java developer that certainly includes installing multiple version of the Java Development Kit.

I have to be honest I am amazed at how many blogs and post exist about utilizing jEnv or Homebrew-Cask. It even appears in Stackoverflow posts that it is the preferred method by many.

I on the other hand really prefer to use a simple approach and not have any additional layers in place (jEnv or Homebrew-Cask or other options). In a distant second place on Stackoverflow is just installing the JDKs and do a little modification to your bash profile to support switching between the versions.

I decided to write this post to try to promote the simple way of supporting multiple versions of Java in your Mac environment. At the time of writing this up I was running macOS Sierra Version 10.12.6.

Here is what the relevant parts of my .profile looks like under my home directory /Users/:

#.profile which controls environment for sh and bash shell
export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)

alias java7='export JAVA_HOME=$JAVA_7_HOME; export PATH=$JAVA_HOME/bin:$PATH'
alias java8='export JAVA_HOME=$JAVA_8_HOME; export PATH=$JAVA_HOME/bin:$PATH'

This allows the default version of java to be managed by the mac os. /usr/bin/java will be symlinked to the Current version of the JavaVM Framework.

/usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

Of note is that the jdks for Java 7 and above are actually installed at /Library/Java/JavaVirtualMachines/

Now if I want to switch which java environment I’m going to use development I can just use the aliases I have defined in my profile.

For example switch to Java 7, simply

$ java7

JAVA_HOME and PATH are now appropriately set to utilize JDK 1.7. Why look for another solution?