In this article we will explorer how to install and manage multiple Java version on MacOS. For this purpose, we will use a well known Java Version Manager software called jEnv.
There are two Java releases
|Feature||Oracle JDK||Open JDK|
Commercial: Oracle Binary Code License Agreement
|Open source: GNU GPL v 2|
|Release Cycle||Every three years (LTS)||Every three months|
|Other Features||Flight Recorder|
Java Mission Control
Application Class-Data Sharing
Better Garbage Collection
We will use Homebrew (the package manager for MacOS) to install the required softwares, open a terminal and run following command to install homebrew on your Mac.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Optional: Remove Existing Java
If you have any existing version of java installed on your machine, open terminal run following commands on you Mac.
sudo rm -rf /Library/Java/* sudo rm -rf /Library/PreferencePanes/Java* sudo rm -rf /Library/Internet\ Plug-Ins/Java*
We will make use of Homebrew Cask (homebrew package to install GUI based applications), cask package is included by default in latest versions of homebew so we not need to install it separately.
Install Java version manager: jEnv
jEnv, a well known Java Version Manager is a command line tool to manage multiple versions of java on your machine, behind the scenes it uses
JAVA_HOME environment variable to do this.
Open a terminal window and run following command to install jEnv on your Mac machine.
brew install jenv
Output of successful installation will be as following.
Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/cask). ==> Updated Casks brave-browser ==> Downloading https://github.com/jenv/jenv/archive/0.5.4.tar.gz ==> Downloading from https://codeload.github.com/jenv/jenv/tar.gz/0.5.4 ######################################################################## 100.0% ==> Caveats To activate jenv, add the following to your ~/.profile: export PATH="$HOME/.jenv/bin:$PATH" eval "$(jenv init -)" ==> Summary 🍺 /usr/local/Cellar/jenv/0.5.4: 82 files, 72.1KB, built in 2 seconds
Now we have jEnv installed on our machine and will add following entries to shell
configuration. Location and name of the shell configuration may vary depending on the currently active shell on your Mac. If you look at the output from last section it actually has the reference to the target shell configuration file i,e.
~/.profile. So let’s open it with
nano text editor.
sudo nano ~/.zshrc
Add following entries in
~/.zshrc file, save and exit the editor using
Ctrl + O then
Ctrl + X then
ENTER key combination. On older versions of MacOS, replace
export PATH="$HOME/.jenv/bin:$PATH" eval "$(jenv init -)" jenv enable-plugin export jenv enable-plugin maven
Now either restart the Terminal or type following command to reload the shell configurations.
Verify the installation of jEnv by running following command.
Successful installation will produce output like following.
OK] No JAVA_HOME set [ERROR] Java binary in path is not in the jenv shims. [ERROR] Please check your path, or try using /path/to/java/home is not a valid path to java installation. PATH : /usr/local/Cellar/jenv/0.5.4/libexec/libexec:/Users/u1/.jenv/shims:/Users/sma/.jenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin [OK] Jenv is correctly loaded
We will activate AdoptOpenJDK tap to install specific version of Open JDK, to do this from terminal run following command.
brew tap AdoptOpenJDK/openjdk
Now let’s start with installing Java 8. Please note that on older versions of MacOS following command will revsied as “
brew cask install adoptopenjdk8“.
brew install --cask adoptopenjdk8
Next install Open JDK version 11 using following command.
brew install --cask adoptopenjdk11
For successful installation the output will be something like following.
Updating Homebrew... ==> Downloading https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/down Already downloaded: /Users/sma/Library/Caches/Homebrew/downloads/1d0d41e7cf8f14ebb4df0ff39fa16e0fe2a1ac85f05ae404c62841c6ff01d4c8--OpenJDK11U-jdk_x64_mac_hotspot_11.0.8_10.pkg ==> Verifying SHA-256 checksum for Cask 'adoptopenjdk11'. ==> Installing Cask adoptopenjdk11 ==> Running installer for adoptopenjdk11; your password may be necessary. ==> Package installers may write to any location; options such as --appdir are i installer: Package name is AdoptOpenJDK installer: Upgrading at base path / installer: The upgrade was successful. package-id: net.adoptopenjdk.11.jdk version: 11.0.8+10 volume: / location: Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk install-time: 1602892594 🍺 adoptopenjdk11 was successfully installed!
Let’s also install the latest available version of java using following command.
brew install --cask adoptopenjdk
Configure JDK version with jEnv
Now we have Java 8, 11 and 16 (latest available version on publish date of this article) installed on our machine.
In this section will learn to configure current JDK version with the help of jEnv.
To enable jEnv switch between different java versions, we need to tel jEnv about all versions installed on our machine. Tun following command to get a list of all versions of java installed on your machine.
This will return list of all installed java versions as following.
Matching Java Virtual Machines (3): 16 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 16" /Library/Java/JavaVirtualMachines/adoptopenjdk-16.jdk/Contents/Home 11.0.11 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home 1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home /Library/Java/JavaVirtualMachines/adoptopenjdk-16.jdk/Contents/Home
Now we will add each of the installed JDK to jEnv as following, please do remember to revise the commands according to path of versions installed on your machine.
jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-16.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
Let’s verify if all of the Java versions are now available to jEnv by running following command.
This will produce output like following.
* system (set by /Users/sma/.jenv/version) 1.8 22.214.171.1242 11 11.0 11.0.11 16 openjdk64-126.96.36.1992 openjdk64-11.0.11 openjdk64-16
Congratulations, you have setup jEnv successfully and can use it to manage your development environments easily.
Set global Java using jEnv
We can use following command to set global / system wide java version.
jenv global 16
Above command will set Java 15 as the system wide Java version, let’s verify it by running java -version command as following.
Following output confirms current java version as Java 15.
openjdk version "16" 2021-03-16 OpenJDK Runtime Environment AdoptOpenJDK (build 16+36) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 16+36, mixed mode, sharing)
Let’s switch our global version to Java 8 by running following command.
jenv global 1.8
Let’s confirm the change by running following command.
And output confirms that now the global java version is Java 1.8.
openjdk version "1.8.0_292" OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)
Set local Java using jEnv
Use following command to set project specific Java version, this will create a file named
.java-version and you can even commit this file in source control to share it with team.
jenv local 16
Set Shell specific Java using jEnv
We can set Java version for currently opened shell using following command.
jenv shell 11