How to set up multiple Java versions on a MacBook?

Ye Min Ko
2 min readAug 10, 2024

--

Photo by Oliver Plattner on Unsplash

Setting up multiple Java versions can sometimes be complicated and time-consuming. However, by using some utilities, it becomes much easier.

Which utilities should we use?

We’ll use Homebrew and jEnv (a multi-Java version manager). If you haven’t installed Homebrew, install it first.

Step 1: Install multi-Java versions

For example, if you want to use java 17 and 21, install them with the following command.

brew install openjdk@17
brew install openjdk@21

Step 2: Create symlinks

Just run the commands after installing both versions of Java to create symlinks.

sudo ln -sfn /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk
sudo ln -sfn /opt/homebrew/opt/openjdk@21/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-21.jdk

Step 3: Install jEnv

Now install jEnv.

brew install jenv

Step 4: Activate jEnv

After that, you need to activate jEnv. To do that, add the following to your shell profile, e.g., ~/.profile or ~/.zshrc .

export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"

Step 5: Enable export plugin

After saving, run the following command to control the JAVA_HOME variable with jEnv.

jenv enable-plugin export

You’ll need to restart your shell by closing and reopening your terminal window.

Step 6: Identify paths for java installations

Run the command to see a list of all installed Java versions along with their paths.

/usr/libexec/java_home -V

The output will look like this:

Matching Java Virtual Machines (2):
21.0.4 (arm64) "Homebrew" - "OpenJDK 21.0.4" /opt/homebrew/Cellar/openjdk@21/21.0.4/libexec/openjdk.jdk/Contents/Home
17.0.12 (arm64) "Homebrew" - "OpenJDK 17.0.12" /opt/homebrew/Cellar/openjdk@17/17.0.12/libexec/openjdk.jdk/Contents/Home
/opt/homebrew/Cellar/openjdk@21/21.0.4/libexec/openjdk.jdk/Contents/Home

Step 7: Add Java versions to jEnv

Time to add each Java version to jEnv. Use the paths from the output of the previous command.

jenv add /opt/homebrew/Cellar/openjdk@21/21.0.4/libexec/openjdk.jdk/Contents/Home
jenv add /opt/homebrew/Cellar/openjdk@17/17.0.12/libexec/openjdk.jdk/Contents/Home

Step 8: Check java versions

You can check added java versions in jEnv with this command.

jenv versions

The output will look like this:

* system (set by /Users/yeminko/.jenv/version)
17
17.0
17.0.12
21
21.0
21.0.4
openjdk64-17.0.12
openjdk64-21.0.4

Step 9: Set the Global Java Version

To set Java 17 as the default version globally, you can run this command. (using the versions from the output of the previous command)

jenv global 17.0.12 

# Or to set Java 21 as global
jenv global 21

Check if it works by running this:

java -version

Set the local or shell Java version (Optional)

For Local (for a directory)

If you want to set a specific Java version for a project, you can do it like this:

cd /path/to/your/project
jenv local 21

Shell (for the current session)

To set the Java version for the current terminal session, run this:

jenv shell 21

Done 🎉

With the help of jEnv, you can easily manage Java versions as needed, and you’ll forget how to set the JAVA_HOME environment variable manually.

FAQ

Maven doesn’t reflect the Java version set by jEnv.

If you installed Maven through Homebrew, you need to uninstall the Java version that was installed along with Maven.

--

--

Ye Min Ko

🌐 Web Developer | 👀 Self-Learner | 👨‍💻 An Introvert