7

Using jenv to select Java version on macOS

 3 years ago
source link: https://akrabat.com/using-jenv-to-select-java-version-on-macos/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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:

$ brew install jenv
$ brew install java
$ brew tap AdoptOpenJDK/openjdk
$ brew cask install adoptopenjdk12
$ brew cask install adoptopenjdk8

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

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

Restart your terminal to pick up the change.

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

$ jenv add /Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Home/
$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-12.jdk/Contents/Home/
$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/

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:

$ jenv versions
* system (set by /Users/rob/.jenv/version)
  1.8
  1.8.0.275
  12
  12.0
  12.0.2
  15
  15.0
  15.0.1
  openjdk64-1.8.0.275
  openjdk64-12.0.2
  oracle64-15.0.1

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:

$ jenv version 12

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!

This article was posted on 2 December 2020 in Java


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK