DEV Community

Noah
Noah

Posted on

java -version Shows the Wrong Version on Mac

You installed a newer JDK, opened a fresh Terminal window, ran java -version, and still saw JDK 8, JDK 11, or a vendor you did not expect. The install probably worked. The more common problem is that the java command is resolving a different JDK from the one you think you selected.

This walkthrough is ordered like a diagnosis rather than an installation guide. You will inventory what macOS can see, follow the shell startup files that set your environment, trace PATH and command caching, check version-manager shims, and finally compare the terminal’s JVM with the one your IDE and Gradle use.

Table of Contents

What java -version Actually Reports

java -version reports the JVM that the shell resolves when it looks up java. It is not an inventory of every JDK on the machine, and it is not necessarily the newest JDK you installed.

On macOS, /usr/bin/java is a system launcher that looks for an installed JDK. Oracle’s macOS JDK installation notes explain that java -version shows the default JDK and that /usr/bin/java will find the newest JDK installed. That default can still be overridden by an older java earlier in your PATH, and a stale JAVA_HOME can affect launchers and build tools that consult it.

This distinction explains a common surprise: echo $JAVA_HOME may point at JDK 17 while java -version prints JDK 11, or JAVA_HOME may be empty while java works perfectly. JAVA_HOME is a convention used by build tools and scripts. The java command itself is found through PATH.

Also compare java -version with javac -version. If they disagree, one of them is coming from a different directory. The same applies to the vendor name and build string: “OpenJDK” and “Oracle” can both be Java 17, but they are not the same installation.

Step 1: Inventory the JDKs macOS Can See

Before changing shell files, establish a baseline. Ask the macOS Java helper what it can find:

/usr/libexec/java_home -V
Enter fullscreen mode Exit fullscreen mode

The capital -V lists the JVMs recognized by the helper. Read the version, architecture, vendor, and full path for each entry. On Apple Silicon, pay attention to arm64 versus x86_64; a correct version number with the wrong architecture can still cause confusing behavior.

If the list is empty, macOS cannot see the JDK yet, so fix that before editing shell files. If the command fails or its output looks wrong, try the same check with JAVA_HOME removed for that one command, since a stale value can affect the helper: env -u JAVA_HOME /usr/libexec/java_home -V.

To test a specific version without changing your environment, use the helper’s -v and --exec options:

/usr/libexec/java_home -v 11 --exec java -version
/usr/libexec/java_home -v 11 --exec javac -version
Enter fullscreen mode Exit fullscreen mode

The Oracle documentation also shows this java_home -v <version> --exec pattern for running a different JDK than the default. If the expected JDK appears in the inventory, the installation itself is probably fine and the problem is selection. If it does not appear, fix visibility first.

If JDK 11 is the version you expect, the JDK 11 macOS installation guide shows the expected install location, JAVA_HOME value, and verification output. That gives you a known-good baseline to compare against before you start editing startup files.

A Homebrew installation is a frequent reason a JDK is missing from this list. Homebrew keeps its OpenJDK formulae keg-only, so they do not replace the system Java tools automatically. Homebrew’s notes on making a JDK visible to macOS explain that the caveats may give you a command to link the JDK bundle into /Library/Java/JavaVirtualMachines, which is where macOS tools look. A typical link command looks like this:

sudo ln -sfn "$(brew --prefix openjdk@11)/libexec/openjdk.jdk" \
  /Library/Java/JavaVirtualMachines/openjdk-11.jdk
Enter fullscreen mode Exit fullscreen mode

Use the path printed by brew info for your formula and Mac. After linking, run /usr/libexec/java_home -V again. Do not assume that brew install openjdk@11 alone made the JDK visible to java_home.

Step 2: Check Which Shell Startup File Runs

macOS has used zsh as the default login and interactive shell since macOS 10.15, and Apple’s guide to zsh as the default shell maps .zprofile to login sessions and .zshrc to each new Terminal session. That matters because an export placed in the wrong file can appear to work only after you manually run source.

The zsh startup file documentation gives the exact order:

  1. /etc/zshenv
  2. ~/.zshenv
  3. /etc/zprofile, then ~/.zprofile, for login shells
  4. /etc/zshrc, then ~/.zshrc, for interactive shells
  5. /etc/zlogin, then ~/.zlogin, for login shells

On a default macOS installation, a new Terminal window starts a login, interactive zsh, so both .zprofile and .zshrc are normally read. A non-interactive shell, such as a script started by a build tool, does not read .zshrc. That is one reason a setting can work in Terminal but not in an IDE or automated build.

Typical symptoms and their file-level causes:

Symptom Likely cause
echo $JAVA_HOME is blank in a new Terminal, but works after source ~/.zshrc The export is only in a file the new shell did not read, or you set it manually in the old shell
You edited .bash_profile but the shell is zsh zsh does not read .bash_profile; use .zprofile or .zshrc
It works over SSH but not in the IDE The IDE may not be using the same login shell environment
It works in one Terminal tab but not another after a cd A directory-specific file or version-manager hook is changing the value

If you use bash instead of zsh, the same ideas apply with different filenames: login shells read ~/.bash_profile, while interactive non-login shells read ~/.bashrc. A bash export in .bash_profile will not help a zsh session, and a zsh export in .zshrc will not help a non-interactive bash script.

To make a version selection persistent, copy the JDK home path from your inventory into the startup file that matches the shell sessions you care about. For example, in ~/.zprofile:

export JAVA_HOME="/Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home"
export PATH="$JAVA_HOME/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

Use the exact path from your inventory; do not guess a version-numbered directory. If you need the value in non-login interactive shells too, put the block in ~/.zshrc instead. Do not add it to both files unless you guard against duplicate PATH entries. Check the final value rather than assuming the file you edited wins, because a later startup file can reorder PATH again.

Step 3: Trace PATH Precedence and Cached Commands

The first matching java on PATH wins. Print the candidates instead of guessing:

echo "$PATH" | tr ':' '\n'
type -a java
type -a javac
Enter fullscreen mode Exit fullscreen mode

type -a shows every match in order, including aliases, functions, and hashed commands. If an old JDK’s bin directory appears before the JDK you want, that old java will win even if JAVA_HOME points somewhere newer.

A stale JAVA_HOME is a common source of this problem. If a startup file contains:

export PATH="$JAVA_HOME/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

and JAVA_HOME still points at JDK 8, that line puts JDK 8 in front of everything else. Change JAVA_HOME first, then rebuild the path, or set the explicit JDK path.

The shell also caches command locations. If you change PATH in the current shell and java still resolves to the old binary, clear the cache. In zsh, whose builtin command reference documents rehash as the same as hash -r, run:

rehash
type -a java
java -version
Enter fullscreen mode Exit fullscreen mode

In bash, use hash -r. Then check type -a java again. A new Terminal window also clears this cache because it starts a new shell.

One more place to look is /usr/bin/java itself. If your PATH contains /usr/bin before any JDK bin directory, the system launcher decides which JDK to use. That may be the newest JDK in /Library/Java/JavaVirtualMachines, but it will not see a Homebrew keg that was never linked there, and it will not see an SDKMAN or jenv installation unless that tool has put its own shims earlier in PATH.

Step 4: Check Version Manager Shims

If you use jenv, the shim directory is probably doing the selection. A shim is a small wrapper that chooses the real JDK and then hands off to it. jenv’s README explains that it sets JAVA_HOME globally, locally, or per shell, and that it needs to be initialized in your shell with eval "$(jenv init -)". The export plugin is what makes jenv set JAVA_HOME to its shim path; enable it with jenv enable-plugin export.

Run the diagnostic first:

jenv doctor
jenv versions
cat .java-version 2>/dev/null
echo "$JENV_VERSION"
Enter fullscreen mode Exit fullscreen mode

jenv doctor reports whether jenv is loaded and whether the java in your path is actually a jenv shim. If it says the Java binary is not in the jenv shims, your PATH order is wrong or jenv is not initialized in this shell.

jenv versions shows the active version with an asterisk and often tells you where it was set. The precedence is global, then local, then shell, with shell highest. Set the level you need with jenv global <version>, jenv local <version>, or jenv shell <version>; jenv local writes the .java-version file. A .java-version file in the project directory can silently pin an old JDK every time you enter that directory. jenv shell affects only the current shell and is easy to forget, so check JENV_VERSION before you edit global settings.

If the JDK was installed but never added to jenv, jenv cannot select it. Add the real JDK home, not the bin directory:

jenv add /Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home
jenv versions
Enter fullscreen mode Exit fullscreen mode

Some other version managers use the same shim principle: their initialization code puts shims ahead of the system paths, and a per-directory version file can override the global default. The useful question is not only “which JDK is installed?” but “which manager is currently in front of PATH, and what version has it selected?”

Step 5: Check the IDE and Gradle JVM

If Terminal reports the right version but your IDE or Gradle build reports an old one, the shell is no longer the only suspect. Gradle’s Java toolchains documentation explains that the IDE has its own Gradle JVM setting, and that this is separate from the project toolchain, which is the set of Java tools for the version the build compiles against. In IntelliJ IDEA, check Settings > Build, Execution, Deployment > Gradle > Gradle JVM. In Eclipse, check Preferences > Gradle > Gradle JDK.

Gradle can use two JVMs: the client JVM that starts the build and the daemon JVM, a long-lived background process that executes it. The Gradle daemon documentation notes that the daemon can be reused only when its Java home, version, and JVM arguments match. If Gradle still reports an old JVM, stop the daemon and inspect again:

./gradlew --stop
./gradlew --version
./gradlew -q javaToolchains
Enter fullscreen mode Exit fullscreen mode

--version shows the JVM running Gradle. The javaToolchains task lists the JDKs Gradle detects, including where each one was found. If the build uses a toolchain, Gradle may compile with JDK 11 while java -version shows JDK 21; that is not a bug. The toolchain is a build-level choice, while java -version reports the shell’s JVM.

Also check for a pinned JDK in gradle.properties. The Gradle daemon documentation identifies org.gradle.java.home as one of the settings that determines the daemon JVM. A project-level or user-level gradle.properties can keep an old path alive long after the shell has moved on. Search both the project root and ~/.gradle/gradle.properties.

If only one project needs the older JDK, prefer a project-level toolchain or a local version-manager setting over a global PATH edit. That keeps the rest of your tools on the default JDK while the project gets the version it needs.

Verification Checklist

Run these in order and compare the answers:

  1. java -version and javac -version — do they report the same version, vendor, and build?
  2. echo $JAVA_HOME and type -a java — does JAVA_HOME point to the same JDK as the first java on PATH?
  3. /usr/libexec/java_home -V — is the expected JDK listed with the right architecture?
  4. echo "$PATH" | tr ':' '\n' — is any older JDK bin directory ahead of the expected one?
  5. rehash in zsh or hash -r in bash — does java -version change after clearing the cache?
  6. jenv doctor, jenv versions, and cat .java-version — is a shim or project file pinning an older version?
  7. ./gradlew --stop, ./gradlew --version, and ./gradlew -q javaToolchains — is Gradle using the JVM you expect?
  8. IDE Gradle JVM setting — does it match the JDK you want the IDE to use?

If the terminal is correct but the IDE is wrong, fix the IDE setting. If the IDE is correct but the terminal is wrong, fix the shell startup file or PATH order. If both are wrong, go back to the inventory and confirm the JDK is actually visible to macOS.

Conclusion

A wrong java -version on macOS usually means the shell is resolving a different JDK, not that the new JDK failed to install. Start with /usr/libexec/java_home -V to see what macOS can find, then check the startup file that actually runs, the order of entries in PATH, and the shell’s command cache. If a version manager is in use, check its shims and per-directory version file. For IDE and Gradle mismatches, check the Gradle JVM setting and stop any stale daemon. Work through those layers in order, and you can fix the version without breaking the rest of your environment.

Top comments (0)