A typical backend engineer touches three Java codebases in a week: a legacy service still on Java 11, the main platform on Java 21 and a new service trying Java 25. Each build expects its own JDK, and the failure when you get it wrong is rarely clear. Maven might report release version 21 not supported, Gradle might fail with Unsupported class file major version 69, or the IDE quietly compiles with one JDK while the terminal runs another.

This guide explains why Java version switching is harder than Node or Python, then walks through jenv and SDKMAN, how each pins a version per project, and how to make the build and CI agree with your shell.

Why Java has two switches, not one

Most runtimes have one thing to switch: the executable found on PATH. Java has two, and they are independent:

Variable

Who reads it

Example

PATH

Your shell, when you type java or javac

/Users/dev/.sdkman/candidates/java/current/bin

JAVA_HOME

Maven, Gradle (for the daemon), Tomcat scripts, many IDE launchers, mvnw/gradlew

/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home

If they point at different JDKs, java -version and mvn -v disagree, which is the most common "it works in my terminal" report we see. Any version manager you choose must set both or you will chase that mismatch forever.

On macOS, Apple's helper lists installed JDKs and prints the home of one:

/usr/libexec/java_home -V
/usr/libexec/java_home -v 21

/usr/bin/java on macOS is a stub that respects JAVA_HOME, which adds a third layer of surprise when it is first on PATH.

Option 1: jenv

jenv does not install JDKs. You install them however you like (Homebrew casks, vendor installers, the Adoptium .pkg) and register them with jenv. It then manages a shim directory on PATH, much like pyenv or rbenv.

brew install jenv
echo 'eval "$(jenv init -)"' >> ~/.zshrc
exec zsh

jenv add /Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
jenv versions

Version selection has three levels:

jenv global 21          # default for your user
jenv local 17           # writes .java-version in the current directory
jenv shell 11           # this shell only

.java-version is a one-line file such as 17. Commit it, and anyone with jenv gets the same JDK when they enter the directory, because the shims resolve the version from the nearest .java-version each time java runs.

The plugin you must enable

Out of the box, jenv only changes PATH. Maven and Gradle read JAVA_HOME, so enable the export plugin:

jenv enable-plugin export
exec zsh
echo "$JAVA_HOME"       # now follows jenv

There are also maven and gradle plugins that wrap those commands so they use jenv's selection. With export enabled you rarely need them. jenv doctor checks the common setup errors, including an existing JAVA_HOME set elsewhere in your dotfiles that fights the plugin.

When jenv fits

  • You want JDKs installed through your normal package manager or by IT.

  • You like the pyenv/rbenv mental model and already use it.

  • You only work on macOS or Linux; jenv has no Windows support.

Option 2: SDKMAN

SDKMAN installs and switches SDKs. "Candidates" include Java plus Maven, Gradle, Kotlin, Scala, sbt, JBang, Quarkus CLI and more. It changes PATH and JAVA_HOME together, because each candidate has a current symlink under ~/.sdkman/candidates/.

curl -s "https://get.sdkman.io" | bash
exec zsh

sdk list java                     # vendors and identifiers
sdk install java 21.0.5-tem       # Temurin 21
sdk install java 17.0.13-tem
sdk default java 21.0.5-tem
sdk use java 17.0.13-tem          # this shell only
sdk current java

Identifiers combine version and vendor (-tem for Eclipse Temurin, -amzn for Corretto, -zulu for Azul Zulu, -graal for Oracle GraalVM, and so on). Pin the exact identifier, not just the major, so everyone gets the same vendor build.

Per-project pins with .sdkmanrc

cd my-service
sdk env init        # writes .sdkmanrc with your current versions

The file looks like:

java=17.0.13-tem
maven=3.9.9

sdk env applies it in the current shell, sdk env install installs anything missing, and sdk env clear reverts to defaults. To apply it automatically on cd, set this in ~/.sdkman/etc/config:

sdkman_auto_env=true

Because .sdkmanrc can pin Maven or Gradle too, it removes a second class of drift: someone on Maven 3.6 hitting plugin errors that nobody else sees. That said, the Maven and Gradle wrappers (mvnw, gradlew) already pin the build tool per project and are usually the better place for that.

When SDKMAN fits

  • You want one tool to install and switch JDKs and JVM build tools.

  • You need several vendors side by side (Temurin, Corretto, GraalVM).

  • You work on macOS, Linux, or WSL on Windows.

jenv vs SDKMAN at a glance

Question

jenv

SDKMAN

Installs JDKs

No

Yes

Sets JAVA_HOME

With export plugin

Yes, always

Per-project file

.java-version

.sdkmanrc

Auto-switch on cd

Yes (shims)

With sdkman_auto_env=true

Other tools (Maven, Gradle, Kotlin)

Plugins wrap them

Installs and switches them

Windows

No

WSL or Git Bash

Model

Shims, like pyenv

Symlinks + PATH edits

Do not run both at once. Each will try to own JAVA_HOME, and the last one initialised in your shell wins in ways that change between new tabs and IDE terminals.

Let the build pick the JDK: Gradle and Maven toolchains

A version manager makes your shell right. A toolchain makes the build right regardless of the shell, which is what you want for CI and for teammates who use a different manager.

Gradle toolchains declare the JDK the project compiles and tests with:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

Gradle then looks for a matching installed JDK (it detects SDKMAN, jenv, asdf and common OS locations) and can download one if you apply the Foojay toolchain resolver plugin in settings.gradle.kts. The Gradle daemon itself still runs on the JDK from JAVA_HOME unless you configure daemon JVM criteria, so keep JAVA_HOME sane anyway.

Maven has the Toolchains plugin with a ~/.m2/toolchains.xml file mapping versions to JDK paths. More setup than Gradle, but it gives the same guarantee. At minimum, set the release level so the compiler fails clearly on the wrong JDK:

<properties>
  <maven.compiler.release>21</maven.compiler.release>
</properties>

An older JDK then fails with release version 21 not supported, which is easy to search and fix.

Keeping CI on the same JDK

actions/setup-java can read the version from a file in the repository, so local and CI share one source of truth:

- uses: actions/setup-java@v4
  with:
    distribution: temurin
    java-version-file: .java-version
    cache: gradle

It also understands .sdkmanrc and .tool-versions, so whichever manager the team uses, the pin is not duplicated in the workflow file. GitLab and other systems usually solve this with a JDK-specific base image; put the major version in the image tag and keep it in sync with .java-version through a CI check that compares the two.

Common mistakes

Mistake

Why it hurts

Do this instead

jenv without the export plugin

Shell and Maven use different JDKs

jenv enable-plugin export

Hard-coded export JAVA_HOME=... in ~/.zshrc

Overrides the version manager in every shell

Remove it, let the manager set it

Pinning java=21 without a vendor

Teammates get different vendor builds

Pin the full identifier, e.g. 21.0.5-tem

IDE project SDK set by hand

IDE compiles with a different JDK than CLI

Point IntelliJ at the toolchain or the managed JDK

jenv and SDKMAN both initialised

Last one wins, differs per terminal

Choose one

Key takeaways

  • Java needs both PATH and JAVA_HOME correct; check java -version and mvn -v or ./gradlew -version together.

  • jenv switches JDKs you install yourself; enable its export plugin.

  • SDKMAN installs and switches JDKs plus build tools; turn on sdkman_auto_env for .sdkmanrc.

  • Gradle toolchains or maven.compiler.release make the build independent of the shell.

  • Feed the same pin file to CI with java-version-file.

References