Manage Java Versions with JEnv

Manage Java Versions with JEnv

Experimenting with different versions of Java made easier by a handy tool.


First, install JEnv itself.

brew install jenv

JEnv simply assist in managing Java versions. We have to install various Java versions by ourselves.

brew tap homebrew/cask-versions
brew cask install java11

Put these two lines in ~/.bash_profile or ~/.zshrc

export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"

After that, just restart the shell for these changes to be picked up

Add Java versions

Find existing Java path, then use jenv add <path>. Where <path> is /Library/Java/JavaVirtualMachines/<version>/Contents/Home. The correct version should be recognized by the tool

$ ls -l /Library/Java/JavaVirtualMachines

total 0
drwxr-xr-x  3 varokas  staff  96 Aug  9  2018 jdk1.8.0_181.jdk
drwxr-xr-x@ 3 varokas  staff  96 Jan 18 00:22 openjdk-11.0.2.jdk

$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
oracle64- added added
1.8 added

$ jenv add /Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home
openjdk64-11.0.2 added
11.0.2 added
11.0 added

We can then inspect all recognized version.

$ jenv versions
* system (set by /Users/varokas/.jenv/version)

Then we can switch between Java versions like so.

$ jenv global 11.0
$ java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

$ jenv global 1.8
$ java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)


The environments variable usually are not as important as long as javac can be executed from shell. Regardless, we could set it like so.

jenv enable-plugin export