How to run multiple Java versions (like Java 8, 11 or latest) on Mac machine

Hemanshu Chauhan
2 min readFeb 9, 2022

--

When it comes to Java development, we generally end up working on different Java projects, built on different Java versions like Java 8, Java 11, Java 16 etc.

Some users end up setting different Java_Home and then exporting them manually in bash or zsh. Here is a smarter way to install & switch different Java versions on Mac machine and increase productivity:

Tool: Jenv

Steps to install and enable multiple Java versions:

  1. Open Terminal (bash or zsh)
  2. First of all install Homebrew (if not installed already)
$ /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

3. Test whether Homebrew installed successfully using this command:

$ brew install wget

4. Now install JENV using homebrew

$ brew install jenv

If you use Bash then run below command

$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile

If you use Zsh then run below command:

$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
$ echo 'eval "$(jenv init -)"' >> ~/.zshrc

5. Now install JDK as needed. For example:

Java 8 :

$ brew install adoptopenjdk8OR use below, in case above command doesnt work:$ brew install --cask homebrew/cask-versions/adoptopenjdk8

Java 11:

Run either this:

$ brew install java11

or this, for Java 11

$ brew install adoptopenjdk11

Java Latest version:

$ brew install java

6. Add environments in JENV

$ jenv add <javaVersionPathHere>

Example command:

$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk

7. You are ready now to use different java versions.

8. Some useful JENV commands:

commands    List all available jenv commandslocal       Set or show the local application-specific Java versionglobal      Set or show the global Java versionshell       Set or show the shell-specific Java versionrehash      Rehash jenv shims (run this after installing executables)version     Show the current Java version and its originversions    List all Java versions available to jenvwhich       Display the full path to an executablewhence      List all Java versions that contain the given executableadd         Add JDK into jenv. A alias name will be generated by parsing "java -version"

References:

--

--