There are many difficulties with Java, specially if you have to use multiple versions on your machine like you usual have to. Coming from Debian, this was quite easy with the alternative mechanisms, but on macOS?
Jenv is a kind of package manager for all your Java versions, which helps you to switch between each of them pretty nicely, once you’ve set it up correctly.
Installation is as easy as open a cold one:
$ brew install jenv
Once done, it reminds you to add it to your shell rc, which is probably a good idea.
Quick side note: Make sure to create the proper directories too, otherwise the commands fail in a weird way:
$ mkdir -p ~/.jenv/version
One java to go
The easiest way to see your installed Java version sis via the java_home command:
$ /usr/libexec/java_home -V Matching Java Virtual Machines (1): 1.8.0_201, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home
So far, nothing much to switch, but we still want to add it to Jenv:
$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home oracle64-22.214.171.124 added 126.96.36.199 added 1.8 added
A quick look into ~/.jenv/versions to check, that it basically created some symlinks to your installation.
Make it two
There is still not much to do, for a Java version manager, when there is only one version. Time to install another one, this time AdoptOpenJdk:
$ brew cask install adoptopenjdk $ brew cask install adoptopenjdk11
And another command to add it to Jenv:
$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home openjdk64-188.8.131.52 added 184.108.40.206 added 11.0 added 11 added
Now we have to versions installed, let’s check it:
$ jenv versions * system (set by /Users/unexist/.jenv/version) openjdk64-220.127.116.11 oracle64-18.104.22.168
And what about java_home:
$ /usr/libexec/java_home -V Matching Java Virtual Machines (2): 22.214.171.124, x86_64: "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home 1.8.0_201, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home
How to use jenv?
All set up done, we can now do the following:
Set a java version
This can be done like this:
$ jenv global openjdk64-126.96.36.199
And verified like:
$ jenv versions system 1.8 188.8.131.52 11 11.0 184.108.40.206 * openjdk64-220.127.116.11 (set by /Users/unexist/.jenv/version) oracle64-18.104.22.168