Hopp til hovedinnhold

Java comes in many versions, if not colors. Different projects and dependencies might rely on different versions. This is where jEnv comes in!

Java versions are easier with jEnv

It seems like it was yesterday I started working on a microservice architecture and saw the need for multiple versions of Java. So it had to be just about a day earlier than yesterday that Even Holte wrote his article in Bekk's Christmas calendar about managing java versions on a mac.

As he writes, jEnv is a neat tool for those us in need of multiple java versions installed at the same time

JEnv

jEnv does not come with its own repository of Java versions, but is a tool for easily changing between versions you have installed yourself.

If you’re on mac or Linux, you can install the official version of jEnv through Homebrew

brew install jenv

Finish your setup by starting jenv on terminal startup by adding it to your .zshrc.

export PATH=”$HOME/.jenv/bin:$PATH”

eval “$(jenv init -)”

To activate jenv, either restart your terminal or source your config file.

source ~/.zshrc

Addons

No tool is complete without a set of addons, is it? jenv comes with a set of addons, and some of them are a must have. The plugin export handles setting JAVA_HOME, a variable used by many a program to refer to the directory of your Java executable. Another key plugin, should you use maven, is the maven-plugin. Of course, gradle comes with a plugin too!

Set them up once by running these two commands

jenv enable-plugin export

jenv enable-plugin maven

jenv enable-plugin gradle

Windows

Windows-developers: rejoice! New since 2021 is that there do exist a windows version! It is made and maintained by someone privately and with a couple more installation steps, but seemingly works as it should 👏

AdoptOpenJdk + Eclipse Foundation == True

In 2020, AdoptOpenJDK which provided JDKs easily installable through Homebrew, is now a part of the Eclipse Foundation as the Eclipse Adoptium Project. As such, newer Java versions can no longer be found on their github or Homebrew. Instead, they can be found on https://adoptium.net/
The latest version can be installed through

brew install — cask temurin

If you want specific versions, first tap into cask/versions before suffixing the version number.

`brew tap homebrew/cask-versions`

`brew install — cask temurin11`

Then, add it to jEnv by executing jenv add with the full path to the Java variable.

Global / Local Java version

Now that everything is set up, you can start working! The easiest way is to set the Java version you use the most as default. For example, with Java 17: jenv global 17 . In the microservice you are using another version, for example Java 15, simply run jenv local 15 . This version will then be added to file in the directory saving your local version for automatically changing Java version any time you enter that directory. If your team all uses jEnv, this file might be nice to commit to git.


Did you like the post?

Feel free to share it with friends and colleagues