Java Version Manager (jenv) is a powerful tool that helps you effortlessly switch Java versions on your system.

Introduction:

In a previous article, we discussed how to easily switch Java versions on MacOS, sparking considerable interest and discussion. In this article, we’ll delve deeper into this topic, introducing advanced techniques and methods to manage your Java development environment more flexibly.

Install and Use Java Version Manager (jenv):

Java Version Manager (jenv) is a powerful tool that helps you effortlessly switch Java versions on your system. First, make sure you have Homebrew installed and then use the following command to install jenv:

brew install jenv

Once installed, initialize jenv with the following commands:

echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc  # If you use zsh
echo 'eval "$(jenv init -)"' >> ~/.zshrc  # If you use zsh
source ~/.zshrc  # Reload the shell configuration

Now, you can list the installed Java versions with:

jenv versions

Set the global Java version with:

jenv global <version>

Alternatively, if you want to set the Java version for a specific project, run:

jenv local <version>

Use Java Version Manager (jenv) Plugins:

jenv provides useful plugins to further extend its functionality. One such plugin is jenv-adoptopenjdk-maven-plugin, which automatically installs AdoptOpenJDK and Maven.

First, install the plugin:

brew install jenv-adoptopenjdk-maven-plugin

Then, enable the plugin in jenv with:

jenv enable-plugin adoptopenjdk-maven

Now, when you use jenv to install a new Java version, the plugin will automatically install the corresponding Maven version.

Customize Java Version Installation Paths:

If you wish to install Java versions to custom paths, either to save system space or for other reasons, you can use the jenv add command. For example:

jenv add /path/to/java/version

example:
jenv add  /Library/Java/JavaVirtualMachines/jdk-11.0.19.jdk/Contents/Home/   

This allows you to include a Java version from a specified path under jenv management.

Use Visual Studio Code Java Plugin:

For Visual Studio Code users, switching Java versions becomes even more straightforward with the Java plugin. After installing the plugin, press F1 and select “Java: Configure Java Runtime,” then choose the Java version you want to use.

These advanced techniques should meet your Java version management needs. Remember to exercise caution when using these tools and techniques to ensure the stability and reliability of your system. We hope these methods enable you to manage your Java development environment more flexibly!

By Ray Lee (System Analyst)

iDempeire ERP Contributor, 經濟部中小企業處財務管理顧問 李寶瑞

One thought on “Exploring Java Version Switching: Advanced Techniques for Easy Java Version Management on MacOS”

Leave a Reply

Your email address will not be published. Required fields are marked *