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

my-macpro:~ timo$ brew cask search "java*"
==> Exact Match
java ✔
==> Partial Matches
eclipse-java java-jdk-javadoc netbeans-java-ee netbeans-java-se yourkit-java-profiler
my-macpro:~ timo$ java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
my-macpro:~ timo$

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:

my-macpro:~ timo$ brew tap caskroom/versions
==> Tapping caskroom/versions
Cloning into '/usr/local/Homebrew/Library/Taps/caskroom/homebrew-versions'...
remote: Counting objects: 196, done.
remote: Compressing objects: 100% (193/193), done.
remote: Total 196 (delta 15), reused 45 (delta 1), pack-reused 0
Receiving objects: 100% (196/196), 78.89 KiB | 4.64 MiB/s, done.
Resolving deltas: 100% (15/15), done.
Tapped 0 formulae (214 files, 295.9KB)

With the ‘versions’ cask tapped, let’s search the casks for “java” again:

my-macpro:~ timo$ brew cask search "java*"
==> Exact Match
java ✔
==> Partial Matches
charles-applejava java-jdk-javadoc java8 netbeans-java-se
eclipse-java java6 netbeans-java-ee yourkit-java-profiler

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:

my-macpro:~ timo$ brew cask info java6
java6: 1.6.0_65
https://support.apple.com/kb/DL1572
Not installed
From: https://github.com/caskroom/homebrew-versions/blob/master/Casks/java6.rb
==> Name
Java Standard Edition Development Kit
==> Artifacts
JavaForOSX.pkg (Pkg)
my-macpro:~ timo$ brew cask info java8
java8: 1.8.0_144-b01,090f390dda5b47b9b721c7dfaa008135
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Not installed
From: https://github.com/caskroom/homebrew-versions/blob/master/Casks/java8.rb
==> Name
Java Standard Edition Development Kit
==> Artifacts
JDK 8 Update 144.pkg (Pkg)
==> Caveats
This Cask makes minor modifications to the JRE to prevent issues with
packaged applications, as discussed here:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=411361

If your Java application still asks for JRE installation, you might need
to reboot or logout/login.

Installing this Cask means you have AGREED to the Oracle Binary Code
License Agreement for Java SE at

https://www.oracle.com/technetwork/java/javase/terms/license/index.html

After tapping the ‘versions’ cask, you can install the aforementioned Java 8, by running:

my-macpro:~ timo$ brew cask install java8
==> Caveats
This Cask makes minor modifications to the JRE to prevent issues with
packaged applications, as discussed here:

  https://bugs.eclipse.org/bugs/show_bug.cgi?id=411361

If your Java application still asks for JRE installation, you might need
to reboot or logout/login.

Installing this Cask means you have AGREED to the Oracle Binary Code
License Agreement for Java SE at

  https://www.oracle.com/technetwork/java/javase/terms/license/index.html

==> Satisfying dependencies
==> Downloading http://download.oracle.com/otn-pub/java/jdk/8u144-b01/090f390dda5b47b9b721c7dfaa008135/jdk-8u144-macosx-x64.dmg
######################################################################## 100.0%
==> Verifying checksum for Cask java8
==> Installing Cask java8
==> Running installer for java8; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are ignored.
Password:
==> installer: Package name is JDK 8 Update 144
==> installer: Installing at base path /
==> installer: The install was successful.
🍺  java8 was successfully installed!

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:

my-macpro:~ timo>env JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
odie-pro:~ timo$ env JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home javac -version
javac 1.8.0_144

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.

7 thoughts on “Installing specific major Java JDK versions on OS X via Homebrew”

Leave a Reply