Install different Java versions on MacOS Mojave 10.14+

0
2755
Install different Java versions on MacOS Mojave 10.14+

There are a lot of updates on Java versions recently, and it is hard to keep track of them. In this post, I will show you a tip to manage and install different Java versions on MacOS Mojave 10.14+.

Install different Java versions

What is the latest Java version at the time of this writing you think?

Well, it is not Java 8, 9, or 10. It’s Java 12 already. Check out the release note of Java 12.

On a fresh new MacBook, try following command:

➜  ~ java -version
No Java runtime present, requesting install.

It will show this prompt dialog,

Check Java versions on fresh MacBook
Check Java versions on fresh MacBook – blog.petehouston.com

Not really nice, we need to access Oracle Java page, login and select a Java version to install. Too many steps just to install Java.

What about using Homebrew to install Java?

Great idea! Mac users/developers should know how to manage software installation via Homebrew theses days.

Check this out:

➜  ~ brew info java
Error: No available formula with the name "java"
Found a cask named "java" instead.
java: 12,33
https://jdk.java.net/
Not installed
From: https://github.com/Homebrew/homebrew-cask/blob/master/Casks/java.rb
==> Name
OpenJDK Java Development Kit
==> Artifacts
jdk-12.jdk -> /Library/Java/JavaVirtualMachines/openjdk-12.jdk (Generic Artifact)

Ah ha, the command brew info java always bring up the latest Java version.

NOTE:
Homebrew always aims to bring latest version of software to install.

In reality, most of Java softwares are still under Java 8, not really upgraded to latest Java version. Therefore, we will need a way to install Java 8.

How to install Java 8?

Try this cask:

$ brew tap caskroom/versions
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
No changes to formulae.

==> Tapping caskroom/versions
Cloning into '/usr/local/Homebrew/Library/Taps/caskroom/homebrew-versions'...
remote: Enumerating objects: 231, done.
remote: Counting objects: 100% (231/231), done.
remote: Compressing objects: 100% (223/223), done.
remote: Total 231 (delta 14), reused 56 (delta 5), pack-reused 0
Receiving objects: 100% (231/231), 95.43 KiB | 204.00 KiB/s, done.
Resolving deltas: 100% (14/14), done.
Tapped 201 casks (249 files, 360.8KB).

Alright, let’s search for all Java formulas:

$ brew search java
==> Formulae
app-engine-java                                 google-java-format                              javarepl                                        jslint4java                                     libreadline-java

==> Casks
charles-applejava                 eclipse-javascript                java                              java11                            java8                             netbeans-java-se                  yourkit-java-profiler
eclipse-java                      font-noto-sans-javanese           java-beta                         java6                             netbeans-java-ee                  oracle-jdk-javadoc                homebrew/cask-versions/java-beta

As you can see, there are java6, java8, and java11, which are LTS versions.

Well, install Java 8 should be done via this command:

$ brew cask install java8

After that, you should have two Java versions, java12 and java8.

In case you want to uninstall a Java formula, use this command:

$ brew cask remove java

It will remove java formula completely from your Mac.

Current Java version can be verified by this:

➜  ~ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    12, x86_64:	"OpenJDK 12"	/Library/Java/JavaVirtualMachines/openjdk-12.jdk/Contents/Home
    1.8.0_202, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/openjdk-12.jdk/Contents/Home

If you install more Java versions, it will show more on output.

The bottom line indicates the current Java JDK being used, it means the version of Java when you type java -version.

In above result, Java 12 is the one being used.

How to switch to Java 8?

java_home works by validating the JAVA_HOME environment variable to determine the current Java version.

Therefore, the only thing we need to do is to config the JDK path for JAVA_HOME variable.

This can be done by following command:

➜  ~ export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

Verify Java 8 version just by:

➜  ~ java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

Yay, it works perfectly.

With this little trick, you can write a Bash function/command to handle the terminal for your Mac development environment.