While trying to get an application analysis tool working on my laptop I discovered that when I originally set it up it only had Java 12 install when I needed to make use of Java 11.
I have never really needed to concern myself with this sort of thing in the past so sprung to Google (and to save me searching again I thought I would summarise my findings). A few things have changed over the last year around Java and Oracle that Oracle are wanting to extract from people (this is a conversation for a different day). The good news is that I had gone with an OpenJDK version on my mac using brew
;
java --version
openjdk 12 2019-03-19
OpenJDK Runtime Environment (build 12+33)
OpenJDK 64-Bit Server VM (build 12+33, mixed mode, sharing)
So how do I see what versions of Java are on my system and how to I change?
Whats on my system?
To see the versions available to you run the following;
/usr/libexec/java_home -V
Matching Java Virtual Machines (1):
12, x86_64: "OpenJDK 12" /Library/Java/JavaVirtualMachines/openjdk-12.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/openjdk-12.jdk/Contents/Home
Homebrew Cask has a whole host of OpenJDK version available to be install;
brew tap homebrew/cask-versions
brew search java
# You can specify the JDK version by using java10, java11 etc. or just java for the latest
brew cask install java11
Now if you rerun the original command in this section to check what versions are available on your system you should see multiple versions.
/usr/libexec/java_home -V
Great, so now I have Java 11 & 12 on my system but how do I make switch versions?
Switching between versions
I found a couple of different ways to achieve this, the method I chose was to play with the $JAVA_HOME
entry in my environment variables. I added the following into my .profile
(.zshrc
in my case and also only adding a line for each version I have installed otherwise you will get errors);
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
export JAVA_10_HOME=$(/usr/libexec/java_home -v10)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_12_HOME=$(/usr/libexec/java_home -v12)
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java9='export JAVA_HOME=$JAVA_9_HOME'
alias java10='export JAVA_HOME=$JAVA_10_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
alias java12='export JAVA_HOME=$JAVA_12_HOME'
# default to Java 12
java12
Now if I want to switch between versions while on the terminal its a matter of typing java
and the version number - java11
, java12
etc.
$ java --version
openjdk 12 2019-03-19OpenJDK Runtime Environment (build 12+33)OpenJDK 64-Bit Server VM (build 12+33, mixed mode, sharing)
$ java11
$ java --version
openjdk 11.0.2 2019-01-15OpenJDK Runtime Environment 18.9 (build 11.0.2+9)OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)