Nurkholis Madjid

Multiple Versions of Java on Mac

I have to admit I do not like Java. Somehow it feels bloated for me. However, I have to have it installed on my computer for some reasons (spoiler alert: work and/or study). You know what makes me even hate it more? I have to install multiple versions of it.

One application I need for study is not compatible with Java 8 yet (it was developed with Java 7 and I have Java 8 installed on my Mac).1 It threw errors when I compiled, and the developers did not bother to reply my email about the incompatibility with the latest version.2 Long story short, I uninstalled Java 8 and re-installed Java 7.

Everything was fine, until I had to install another Java application, again for study reason. This time it required the latest version of Java, i.e., Java 8. It wouldn’t compile with Java 7, and I had no good reasons to ask the developers to make it compatible with older version. So, I had no choice other than installing both versions on my Mac.

There is the cleanest way to do that as explain here. However, I didn’t care how clean it was, I just wanted to have it done in a simplest way possible. Turns out, you can have multiple Java versions on your Mac with just downloading both jdk versions into /Library/Java/JavaVirtualMachines directory.

However, there can only be one Java Virtual Machine running at a time. So, you need to specify which version you want to run in your environment variables. This is what you need to add in your .bash_profile3:

export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)

alias java7='export JAVA_HOME=$JAVA_7_HOME'
alias java8='export JAVA_HOME=$JAVA_8_HOME'

#default java8

With this profile script (which I grabbed from one of the comments in this Stack Overflow thread), the default version is Java 8. You can switch to Java 7 by typing java7 in your Terminal, and go back to Java 8 with java8.

  1. I don’t even remember when I updated it to version 8, but I always click yes whenever there’s an update alert.

  2. I couldn’t complain though, it’s an open source project for academic purpose.

  3. Or .profile or whatever your profile-file’s name in your home directory is. And do not forget to run . .profile to apply the changes to your environment instantly without having to relaunch your Terminal.

More posts on the archive.

Any comments, I'm @_nmadjid on Twitter and Instagram.