Jenv on Fish

jenv is a Java environment manager. It’s not quite as simple to set up with fish shell so this is my method. This assumes macOS and Homebrew.

Installation

To begin with install jenv and link the related function into your fish config directory.

brew install jenv

ln -s /usr/local/opt/jenv/libexec/fish/jenv.fish ~/.config/fish/functions/jenv.fish
ln -s /usr/local/opt/jenv/libexec/fish/export.fish ~/.config/fish/functions/export.fish

Then add the following to your ~/.config/fish/config.fish file. If you are already adding things to PATH you can combine the set with that.

set -x JENV_ROOT /usr/local/opt/jenv
set PATH $HOME/.jenv/bin $PATH
status --is-interactive; and source (jenv init -|psub)

Installing Java

If you don’t have Java already then AdoptOpenJDK can be installed via Homebrew. From their documentation:

brew tap AdoptOpenJDK/openjdk
brew cask install <version>

I’m installing adoptopenjdk111 to go with adoptopenjdk8 that I already have installed.

brew cask install adoptopenjdk11

Configuration

Reload your shell (or open a new one) and check the jenv configuration.

jenv doctor

This should give an error that the Java binary in your path is not in the jenv shims. So let’s fix that. To register the JDKs you have installed:

jenv add <path to JDK>

For the versions I have installed via AdoptOpenJDK I used:

jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

Running ‘jenv doctor’ now tells me everything is OK, which is nice. But we need to set a Java version to use:

jenv global 11.0

This sets JDK 11 as the global version. If you check the java version it should now report as the current AdoptOpenJDK 11.

One thing this is not currently doing for me is setting JAVA_HOME but my tools don’t currently seem to need this.

Using

Finding out what versions you have installed2:

jenv versions

To set the local version for a directory:

jenv local <version>

To set the version for your shell:

jenv shell <version>

  1. JDK 14 isn’t completely supported by what I want to do yet ↩︎

  2. Each JDK shows multiple versions so you can specify at different levels. This allows you to use a specific JDK release or more generally the latest version you have installed of a JDK release. ↩︎