[macOS] Manage Java versions by jEnv

[macOS] Manage Java versions by jEnv

2023-08-03 hit count image

Let's see how to use jEnv to manage the various versions of Java on the same macOS machine.

Outline

When developing multiple Java projects on a single machine (PC), there are cases where you need to build a development environment depending on the Java version used in each project.

In this case, you cannot delete Java and install the Java version required for the project each time. In this blog post, I will introduce how to use jEnv to install and manage various Java versions on a single machine in macOS.

Install jEnv

Execute the following command to install jEnv.

brew install jenv

Then, run the following command to activate the export plugin and the maven plugin.

jenv enable-plugin export
jenv enable-plugin maven

Also, run the following command to use the Java version managed by jEnv.

echo 'export alias java=$HOME/.jenv/shims/java' >> ~/.zshrc`

Java version list

Run the following command to check the Java versions that can be installed.

brew search openjdk

Then, you can see the list of installable Java versions as follows.

==> Formulae
openjdk ✔              openjdk@11 ✔           openjdk@17 ✔           openjdk@8              openj9                 openvdb

==> Casks
adoptopenjdk                                                           adoptopenjdk/openjdk/adoptopenjdk14
adoptopenjdk/openjdk/adoptopenjdk-jre                                  adoptopenjdk/openjdk/adoptopenjdk14-jre
...

Install OpenJDK

In this blog post, I will install 11 and 17 versions of OpenJDK. Run the following command to install the OpenJDK 11 version.

brew install openjdk@11

After the installation is complete, use the following command to register the installed JDK to jEnv.

sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
echo 'export PATH="/usr/local/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc
export CPPFLAGS="-I/usr/local/opt/openjdk@11/include"
jenv add /usr/local/Cellar/openjdk@11/11.0.19/libexec/openjdk.jdk/Contents/Home

Then, install the OpenJDK 17 version in the same way and register it to jEnv.

brew install openjdk@17

sudo ln -sfn /usr/local/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk
echo 'export PATH="/usr/local/opt/openjdk@17/bin:$PATH"' >> ~/.zshrc
export CPPFLAGS="-I/usr/local/opt/openjdk@17/include"
jenv add /usr/local/Cellar/openjdk@17/17.0.7/libexec/openjdk.jdk/Contents/Home
jenv add  /usr/local/Cellar/openjdk/20.0.1/libexec/openjdk.jdk/Contents/Home

jenv versions

After installing the Java versions, run the following command to check the available Java versions.

jenv versions

Then, you can see the OpenJDK 11 and OpenJDK 17 that you just installed as follows.

* system (set by JENV_VERSION environment variable)
  11.0
  11.0.19
  openjdk64-11.0.19
  openjdk64-17.0.7

jenv version

Run the following command to check the current Java version set by jEnv.

jenv version

Then, you can see the current Java version selected by jEnv as follows.

system (set by JENV_VERSION environment variable)

To check the actual Java version, run the following command.

java --version

Then, you can see the actual Java version as follows.

openjdk 20.0.1 2023-04-18
OpenJDK Runtime Environment Homebrew (build 20.0.1)
OpenJDK 64-Bit Server VM Homebrew (build 20.0.1, mixed mode, sharing)

Use installed Java version

Now, let’s see how to change the Java version using jEnv. If you run the following command, you can use OpenJDK 17 globally.

jenv global openjdk64-17.0.7

If you run the following command, you can use OpenJDK 17 only in the current project.

jenv local openjdk64-17.0.7

This command creates a .java-version file and, if jEnv is enabled in the development environment, jEnv automatically changes to use the version.

If you run the following command, you can use OpenJDK 17 only in the current shell.

jenv shell openjdk64-17.0.7

.java-version file

As mentioned earlier, if you run the jenv local command, you can see that the .java-version file is automatically created. This automatically changes the version of Java in the development environment using jEnv.

Therefore, if you use this .java-version file, you can share the Java version required to run the project. If the .java-version file of the current project does not exist, create the .java-version file and write the Java version required for the project as follows.

openjdk64-17.0.7

Completed

Done! We’ve seen how to use jEnv to use various Java versions on the same machine. If you need a development environment for various Java versions, try using jEnv to manage the versions.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts