Developing software in the Real World

Using jenv to select Java version on macOS

When working on OpenWhisk, I discovered that it needed a different Java to the one I had installed. Looking around the Internet, I discovered jenv which shouldn’t have surprised me as I use pyenv and I’m aware of rbenv too.

As I use Homebrew, these are the commands I used.

Firstly install jenv, the latest Java (15 at this time) and any other versions you need. Java 8 and 12 in this example:

We now add jenv to our terminal by adding the following to .bash_profile:

Restart your terminal to pick up the change.

The next step is to add our Java versions to jenv:

The exact versions depend on what you have installed. ls /Library/Java/JavaVirtualMachines/ will tell you.

Selecting the Java version

jenv’s versions will provide you a list of the available versions on your system:

Update: with newer version of jenv, use jenv local

I like to set a given Java version on a per-directory basis using jenv local. For example to set Java 12 for OpenWhisk, I navigate to ~/Project/openwhisk and type:

This will create a .java-version file in the directory with the specified version.

Whenever you I navigate to this directory or a sub-directory of it, then jenv will ensure that Java 12 will be used.

Job done!

3 thoughts on “Using jenv to select Java version on macOS

  1. The 'brew cask install' command is now longer correct, it should be 'brew install –cask'

    The 'jenv version' command only gives you the current version. The command should be 'jenv local'.

Thoughts? Leave a reply

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