LinuxCommandLibrary

jenv

Manage multiple Java Development Kit installations

TLDR

Add a Java version to jEnv

$ jenv add [path/to/jdk_home]
copy

Display the current JDK version used
$ jenv version
copy

Display all managed JDKs
$ jenv versions
copy

Set the global JDK version
$ jenv global [java_version]
copy

Set the JDK version for the current shell session
$ jenv shell [java_version]
copy

Enable a jEnv plugin
$ jenv enable-plugin [plugin_name]
copy

SYNOPSIS

jenv <subcommand> [arguments...]
Common subcommands include: add, remove, versions, global, local, shell, enable-plugin, disable-plugin, rehash, doctor.

PARAMETERS

add <JDK_HOME>
    Registers a JDK installation (e.g., /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home) with jenv, making it available for selection.

remove <name>
    Removes a registered JDK by its unique name (e.g., 17.0.10).

versions
    Lists all JDK versions known to jenv, highlighting the currently active global, local, and shell versions.

global <version>
    Sets the default Java version for all shells and directories where a specific version isn't otherwise specified.

local <version>
    Sets the Java version specific to the current directory and its subdirectories. This creates a .java-version file.

shell <version>
    Sets the Java version for the current shell session only. This overrides global and local settings.

enable-plugin <plugin>
    Activates an optional jenv plugin (e.g., export, maven, gradle) to extend functionality.

disable-plugin <plugin>
    Deactivates a previously enabled jenv plugin.

plugins
    Displays a list of all available jenv plugins and indicates which ones are currently enabled.

rehash
    Rebuilds the jenv shim executables, ensuring all changes (like new JDKs or enabled plugins) are recognized. Crucial after adding a JDK or enabling a plugin.

doctor
    Diagnoses common issues with your jenv setup, providing recommendations for fixes.

version
    Shows the active Java version that jenv has selected for the current context.

DESCRIPTION

jenv is a powerful command-line utility designed to simplify the management of multiple Java Development Kit (JDK) versions on a single system. It empowers developers to seamlessly switch between different JDKs, ensuring that each project operates with its specific required Java version without conflicts. jenv achieves this by inserting shims into your PATH environment variable. These shims intercept commands like java, javac, maven, and gradle, delegating them to the correct JDK based on global settings, project-specific configurations, or current shell session preferences. This eliminates the tedious process of manually configuring JAVA_HOME and prevents version clashes, making it indispensable for environments where diverse Java projects coexist. jenv integrates smoothly with popular build tools and provides a clean, efficient way to handle your Java environments.

CAVEATS

jenv requires proper shell initialization by sourcing eval "$(jenv init -)" in your shell's configuration file (e.g., ~/.bashrc, ~/.zshrc).
For jenv to function correctly, any manually set JAVA_HOME environment variable must be unset before jenv is initialized, as jenv manages this itself.
jenv does not install JDKs; it only manages existing installations. Users must acquire JDKs separately.

<B>INSTALLATION AND SETUP</B>

To use jenv, you first need to install it (e.g., via Homebrew on macOS, or by cloning its Git repository). After installation, it's crucial to add eval "$(jenv init -)" to your shell's startup file (like ~/.bashrc, ~/.zshrc, or ~/.config/fish/config.fish). This command initializes jenv, adds its shims directory to your PATH, and sets up necessary shell functions, enabling jenv's core functionality.

<B>PLUGIN SYSTEM</B>

jenv's capabilities are extensible through a robust plugin system. Key plugins include export, which automatically sets the JAVA_HOME environment variable to the currently selected JDK (essential for many IDEs and build tools that expect it); and maven / gradle, which re-shim the mvn and gradle commands to ensure they use the correct Java version without manual intervention. Plugins must be explicitly enabled using jenv enable-plugin <plugin_name> and changes applied with jenv rehash.

HISTORY

Inspired by the success of version managers like rbenv (Ruby) and pyenv (Python), jenv emerged to address the need for a similar, streamlined Java version management solution. Before jenv, developers often relied on manual JAVA_HOME adjustments, which could be cumbersome and error-prone when juggling multiple projects with different Java version requirements. jenv aims to simplify this process, offering a lightweight and efficient alternative focusing solely on environment switching, rather than comprehensive SDK installation.

SEE ALSO

sdkman(1), nvm(1), rbenv(1), pyenv(1)

Copied to clipboard