Using jEnv to switch JDK versions

Advertisements

Since some years ago, Java has changed the version cadence to more frequent releases. Every 6 months there is a new Java version (with smaller changes or new preview features) and every 3 years there is an LTS (Long Term Support) version which enables officially most of the features implemented on previews versions. In Sept 2018 Java 11 was released and in Sept 2021 Java 17 will be released.

It might be very common to require more than one Java version on your machine, switching from Java 8, to Java 11 or Java 17, or any other version, while you are working on different projects or applications. One way to switch Java’s version is setting the JAVA_HOME env-var to the installation directory of the desire version.

To help with that work jEnv is a tool that you must use.

What’s jEnv?

jEnv is a command line tool to help you forget how to set the JAVA_HOME environment variable

jenv.be

jEnv is a cool tool that helps you switch between different java versions in your development machine. Unfortunately, the steps in http://www.jenv.be/ are not enought when you work with gradle or maven. That’s why this article will indicate the required steps to make it work like a charm.

Installing jEnv

Advertisements

Let’s follow the installation steps in the official documentation.

$ brew install jenv
$ git clone https://github.com/jenv/jenv.git ~/.jenv

Then, we need to add the /bin directory of jEnv to the PATH env-var.

$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile

And, adding the installed java directories to jEnv, using the jenv add <directory> command.

$ jenv add /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
  oracle64-1.6.0.39 added
$ jenv add /Library/Java/JavaVirtualMachines/jdk17011.jdk/Contents/Home
  oracle64-1.7.0.11 added

Finally, let’s enable some plugins to make it work with maven or gradle. This is the step that is not included in the official guide.

$ jenv enable-plugin export
$ jenv enable-plugin maven
$ jenv enable-plugin gradle

That’s all, every thing is read to start using jEnv. We can verify the java version using the commands jenv global and java -version to verify the are using the same java version.

jEnv commands

  • jenv add <jdk bin directory> adds a new version to jenv
  • jenv remove <version> removes the version from jenv
  • jenv global shows the current version used by jenv
  • jenv global <version> sets the indicated version
  • jenv local <version> sets the indicated java version to the local directory.
  • jenv versions lists all the added jdks to jenv

Reference

Advertisements

Leave a Reply

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