In an earlier post, I described how to install the latest version of the Oracle Java JDK using homebrew. What hadn’t been completely obvious to me when I wrote the original blog post is that the ‘java’ cask will install the latest major version of the JDK. As a result, when I upgraded my JDK install today, I ended up with an upgrade from Java 8 to Java 9. On my personal machine that’s not a problem, but what if I wanted to stick with a specific major version of Java?
Turns out, there is actually another cask called “versions” that allows you to do exactly that. With just the ‘java’ cask installed, searching casks for “java” results in the following output
As you can see, it finds a single ‘java’ cask, which is the one I used in the previous blog post to install the current version of the JDK. Now let’s tap the “versions” cask as well:
With the ‘versions’ cask tapped, let’s search the casks for “java” again:
You can see that the search now finds a few more casks, namely “charles-applejava”, “java6” and “java8”. Running ‘brew cask info’ on the two java casks shows that they’ll allow you to install the latest Java 6 and Java 8 JDKs:
After tapping the ‘versions’ cask, you can install the aforementioned Java 8, by running:
At this particular point, the default Java still points to /usr/bin/java. This launcher binary will start Java 9 by default. However, by pointing JAVA_HOME to one of the other installations, you can use the newly installed, latest version of Java 8:
I wouldn’t be surprised if there is an easier way to start Java 8 or Java 6 after the installation of the version specific casks, but the above was a good enough starting point for my needs, plus I can always code up a little switcher utility in a shell script.
If you have an easier way to change Java versions that were installed using the various casks, please let me know in the comments.