Manage Java Versions with jEnv

Master your Java Environment

Jayson GCS
Javarevisited

--

Photo by Nick Grant on Unsplash

Introduction

There are times where we are required to work on multiple Java projects and find ourselves needing to switch between different Java versions. It can be pretty troublesome to manage different Java versions without some quick and reliable tools.

Today, we will be looking at one such command line tool called jEnv, to improve the developer experience for the Java version management workflow.

Installation

On MacOS

brew install jenv

On Linux

git clone https://github.com/jenv/jenv.git ~/.jenv
# Shell: bash
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(jenv init -)"' >> ~/.bash_profile
# Shell: zsh
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(jenv init -)"' >> ~/.zshrc

On Windows

jEnv supports Linux and MacOS only, but it is possible to install it on Windows by installing Windows Subsystem for Linux.

Alternatively, there is another similar project called “JEnv-for-Windows” , which is written in cmd and powershell with similar syntax to fulfil the same purpose.

Verify installation

Run the below command to verify the installation.

jenv doctor

In my case, I am using ZSH shell for my terminal. So if we see the error “Jenv is not loaded in your zsh”, follow the suggested command to update the .zshrc config file and restart my ZSH session.

Take note that for some Apple Silicon Mac, you may need to include an additional command to setup the path before running “echo eval “$(jenv init -)” >> ~/.zshrc” (reference: link)

# Take note to run below command if you are running on Apple Silicon Mac
# echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc

# Append jEnv init config to .zshrc
echo eval "$(jenv init -)" >> ~/.zshrc
# Restart ZSH session
source ~/.zshrc
# Alternative way to restart
exec $SHELL -l

Also, in order for jEnv to automatically set our JAVA_HOME, run the following commands to enable export plugin

# Enable export plugin
jenv enable-plugin export
# Restart ZSH session
source ~/.zshrc
# Alternative way to restart
exec $SHELL -l

Add Java to jEnv

Next step, let us add Java to our newly installed jEnv.

Existing Java Setup

If we have an existing Java setup, we may just simply add it to jEnv without additional installation. Let’s run the following command to find out.

/usr/libexec/java_home

So to add existing Java installation to jEnv, simply run the below command.

jenv add "$(/usr/libexec/java_home)"

Finally, run the next command to verify if we have successfully loaded the intended Java to jEnv.

jenv versions

Other existing Java installation

If we know that we have multiple Java versions installed locally, we can add them to jEnv. In my case, I am using “openjdk” so I perform the following steps to retrieve all my existing “openjdk” installations. My example here is based on “brew”, as my example setup is done in MacOS, do adjust the steps accordingly if you are using Linux or Windows.

# For instance, I have existing Java installations via brew
brew ls | grep 'openjdk'
# Find out the path
brew --cellar openjdk@17

Navigate to the ../Home sub-directory and add that path to jEnv.

jenv add /opt/homebrew/Cellar/openjdk@17/17.0.8.1/libexec/openjdk.jdk/Contents/Home/

Verify that all the intended versions are correctly added to jEnv.

Switch between desired Java versions

Finally, we have a convenient setup to switch between Java versions seamlessly.

# Switch the local directory to the desired Java version
jenv local 11.0.20.1
# Verify the currently selected Java version
cat .java-version

There are also other ways to manage the Java version in the global and shell scope.

# Setting global Java version
jenv global 11.0.20.1
# Setting shell Java version
jenv shell 11.0.20.1

Plugins

There are also various plugins included with jEnv. For examples, we may also use jEnv to manage our Maven and Gradle.

Feel free to play around with various plugins, if you find that the plugin does not fulfil your needs, we can also disable it afterwards via “disable-plugin” option.

# Enable plugin for maven
jenv enable-plugin maven
# Disable plugin for maven
jenv disable-plugin maven

More information regarding the supported plugins.

Finally

Thanks for reading and I hope this article may offer some valuable information to you. I look forward to your following and feel free to check out my other articles here.

--

--