Manage multiple java versions on macOS using brew and jenv
May 20, 2018
5 minute read
Install and Update on macOS
Manage multiple java versions on macOS can be tricky. There’s different ways to install:
homebrew cask
download java distribution from Oracle
download java distribution from java.com
And once the java version becomes outdated, there’s different ways to upgrade too, which we will discuss below.
Upgrade from brew cask
homebrew cask doesn’t offer the ability to upgrade a package, as brew cask upgrade <pkg-name> is not even available, see details.
The closest alternative is homebrew cask reinstall <pkg-name>
Upgrade from “Java Preference Panel”
Be cautions about this approach as it may leave you multiple java versions in multiple places.
A homebrew cask install will download the latest package at /usr/local/Caskroom/java/, then install to both directories, whereas if you install the package you downloaded from java.com, or upgrade the package from ‘Java Preference Panel’, it only installs/upgrades the Web Applet plugin.
There’s also /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/ folder, which maintains a copy of bin commands in current java installation, and it will be updated when a new version of jdk is installed.
When you download Java from Oracle, it only installs the Web Applet Plugin. If you installed the Java 1.8 JDK previously,
then later installed the applet plugin, they could be at two different versions.
In essence, there are two parts to Java on OS X. There is a web plugin(JRE) and a JDk. They are entirely separate components.
The direct download at Java.com will only install the web plugin(JRE).
The JDK download will install both the JVM and the web plugin.
The following is the output of uninstall java:
This shows a brew cask uninstall uninstalls both jdk and jre.
Then I installed latest java8 using brew cask install java8(brew tap caskroom/versions first). Then all 3 locations show the latest version:
Install and maintain multiple java versions including java 7
Install another version of java(java 7) from Oracle
Why not use brew cask you may ask, see github issues for details.
It is fairly easy to install java 7 from Oracle though:
manually download java 7. Reason:
manually install the dmg file.
My observation is that when existing java is already installed, trying to install a different version with dmg file from Oracle is pretty non-destructive:
There is one more entry is added
But other than that, nothing is changed
In particular, /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/ is not updated to the new version.
Install jenv to manage multiple java version
First, we need to install jenv
Next we need to configure jenv to manage multiple java version. We add them into jenv:
Then set one as global:
Finally, after restarting manually, we verify that $JAVA_HOME has been exported by jenv correctly
Install java 10
As the time of writing, brew cask install java installs java 10.
Install:
Verify nothing has changed except for a new entry is added into java_home
Add java 10 into jenv
List current versions in jenv
So exactly what does jenv global <java-version> do behind the scene? It does the following:
It sets the new in `~/.jenv/version` file which current `java` in the `Path` is reading from
It updates $JAVA_HOME(requires restart terminal to take effect)
It updates /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/ to point to new java version(my observation)