How to Change Java Version in MacOS

Rupam Yadav Feb 02, 2024
  1. Use the Traditional Way to Change Java Version in MacOS
  2. Use jenv to Change Java Version in MacOS
How to Change Java Version in MacOS

In this article, we will discuss how to change the version of Java installed on a mac machine. When multiple Java installations of different versions are available on the local machine, we can switch between them using the following steps.

Use the Traditional Way to Change Java Version in MacOS

When installing the JDK package on our mac, we have to specify its path as an environment variable to access the Java tool anywhere in the local system. This environment variable is where we tell the version of Java to be used as default.

In the following steps, we will change the value of the default Java environment variable using the command line.

  1. Check all the installed Java versions on the local mac.

First, we need to change the current directory to /usr/libexec using the cd command.

> cd /usr/libexec

Once we are in the /usr/libexec directory, we execute the ./java_home command with -V that returns the versions and locations of the installed Java.

> ./java_home -V

Here is the output after running the command. We can see three versions of Java available; we focus on the first and the last Java version.

The first Java version is 14.0.1, while the second Java version is 1.8.

Output:

> 14.0.1 (x86_64) "Oracle Corporation" - "Java SE 14.0.1" /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home

1.8.202.08 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
  1. Now, we get the current default Java version used in the mac.

We use the command java with -version that returns the version of the default JDK.

> java -version

Output:

> java version "14.0.1" 2020-04-14
Java(TM) SE Runtime Environment (build 14.0.1+7)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)

We can see the default JDK is Java version 14.0.1; if we want to change this version to 1.8, we need to follow the further steps.

  1. Change the Java version to Java 1.8 version.

Now we change the environment variable JAVA_HOME to a new value. JAVA_HOME is the variable that sets the system default for Java.

We use the export command with the variable name next to it and then specify the location of usr/libexec/java_home with the argument -v and the version we want to change to 1.8.

Note that backticks are being used around the path.

> export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
  1. Check the Java version again to confirm that the version was changed.

We use the java -version command to check the current Java version, and when we execute it, the new version returns in the output.

> java -version

Output:

> 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)

Use jenv to Change Java Version in MacOS

The jEnv is a command-line utility that makes the setup of environment variables easy.

  1. Install the jenv tool using the Homebrew package management system.

We use the below command to install jenv on the local machine.

> brew install jenv
  1. Complete the installation by setting the variables to the shell.

To use the jenv tool, we need to set the variables with the command shell. In our case, we use the zsh shell to use the below commands.

> echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
> echo 'eval "$(jenv init -)"' >> ~/.zshrc
  1. Configure jenv to include the installed Java versions.

We add the Java versions we want to the jenv list. To do this, we use the jenv command with add and specify the location of the JDK.

We have two Java installed, so we run the command twice with their locations.

> jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home
> jenv add /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home
  1. Check the versions of Java in jenv.

We check all the added Java versions using the below command.

> jenv versions

The command output shows all the versions of Java available in the jenv list. Here, 1.8 is the current default Java version.

Output:

> system
* 1.8 (set by /Users/Anju/.jenv/version)
  1.8.0.202
  14
  14.0
  14.0.1
  oracle64-1.8.0.202
  oracle64-14.0.1
  1. Change the Java version from 1.8 to 14.0 globally.

Now we change the version to 14.0 using the below command. Notice that we use global to set the change globally.

> jenv global 14.0

When we check the Java default version, we get the changed Java version (Java version 14.0.1).

> java -version

Output:

> java version "14.0.1" 2020-04-14
Java(TM) SE Runtime Environment (build 14.0.1+7)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)

We can only change the Java version in the current session instead of global change using the following command.

> jenv shell 14.0
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Version