On This Page
You've already done the obvious things. You installed JDK 17 (or 21, or 25), added its bin folder to PATH, maybe set JAVA_HOME too, opened a fresh terminal, and typed java -version. It printed 1.8.0 anyway. So you reinstalled, and it still printed 1.8.0.
The JDK is fine. Your PATH has the right folder in it. The wrong Java version runs because another folder with a java in it comes earlier, and the shell stops at the first one it finds. Sometimes there's a twist on top: a symlink you forgot about, or a shell that remembered the old location and never looked again.
java -version still says 1.8
Here's the shape of it, from a lab shell where JDK 17 is on PATH but not first:
$ echo $PATH
/opt/lab/jdk8u472-b08-jre/bin:/usr/bin:/opt/lab/jdk-17.0.17+10/bin
$ java -version
openjdk version "1.8.0_472"
OpenJDK Runtime Environment (Temurin)(build 1.8.0_472-b08)
OpenJDK 64-Bit Server VM (Temurin)(build 25.472-b08, mixed mode)
Three javas live on that PATH: an old Java 8 JRE, whatever /usr/bin/java links to (JDK 21 on this box), and the JDK 17 I wanted. The one I wanted is last. It never had a chance.
You'll meet this on laptops and dev boxes, usually right after installing a second JDK. A build compiling with one version while your terminal reports another is a different problem (JAVA_HOME), and the JAVA_HOME post covers it.
Which java is your shell running?
Don't guess. Ask for every match, in order:
type -a java # bash: every java on PATH, first one wins
which -a java # same idea, external command
whence -a java # zsh (zsh also has a builtin called where)
where.exe java # Windows cmd or PowerShell
In PowerShell, plain where is an alias for Where-Object and prints nothing useful, so type where.exe, or use Get-Command java -All.
Then ask the java that won where it thinks it lives:
$ readlink -f "$(command -v java)"
/opt/lab/jdk8u472-b08-jre/bin/java
$ java -XshowSettings:properties -version 2>&1 | grep java.home
java.home = /opt/lab/jdk8u472-b08-jre
readlink -f follows every symlink to the real file, which matters on Linux, where /usr/bin/java is almost always a link to a link. java.home is the JVM's own answer and the one I trust most. My first guess when I saw 1.8 on a box like this was that the JDK 17 tarball was broken. Running /opt/lab/jdk-17.0.17+10/bin/java -version by full path printed 17 straight away, which killed that theory in about four seconds.
Four javas, one PATH, in a throwaway shell
You can watch all of this on any Linux box without touching your real setup. I used Ubuntu 24.04 with the distro's OpenJDK 21.0.13.1, plus Temurin 17.0.17+10 and a Temurin 8u472 JRE unpacked into /opt/lab. Every shell below starts with env -i, so nothing from your profile leaks in.
1. Old JRE first, new JDK last.
$ J8=/opt/lab/jdk8u472-b08-jre; J17=/opt/lab/jdk-17.0.17+10
$ env -i HOME=$HOME PATH="$J8/bin:/usr/bin:$J17/bin" bash --norc --noprofile -c 'type -a java'
java is /opt/lab/jdk8u472-b08-jre/bin/java
java is /usr/bin/java
java is /opt/lab/jdk-17.0.17+10/bin/java
Swap the order ($J17/bin first) and java -version prints openjdk version "17.0.17" 2025-10-21. Nothing else changed.
2. JAVA_HOME doesn't move the needle.
$ env -i HOME=$HOME PATH="$J8/bin:/usr/bin" JAVA_HOME="$J17" bash --norc --noprofile -c 'java -version 2>&1 | head -1'
openjdk version "1.8.0_472"
The java launcher doesn't read JAVA_HOME to decide what to run. Your shell picked the binary before Java started.
3. The shell remembers. This one caught me. Put an empty ~/labbin first on PATH, run java once, then drop a JDK 17 link into ~/labbin:
$ env -i HOME=$HOME PATH="$HOME/labbin:/usr/bin" bash --norc --noprofile -c '
java -version 2>/dev/null
ln -s /opt/lab/jdk-17.0.17+10/bin/java ~/labbin/java
echo "which says: $(which java)"; type java
java -version 2>/tmp/v; head -1 /tmp/v
hash -r; java -version 2>/tmp/v; head -1 /tmp/v'
which says: /root/labbin/java
java is hashed (/usr/bin/java)
openjdk version "21.0.13.1" 2026-11-17
openjdk version "17.0.17" 2025-10-21
which said 17. The shell ran 21. Bash keeps a hash table of where it found each command, and it won't search PATH again while the remembered file still exists. hash -r clears it. So does any assignment to PATH, even PATH="$PATH". A new terminal starts with an empty table, which is why "open a new window" works so often without anyone knowing why.
4. The alternatives link. Register JDK 17 with update-alternatives at a lower priority than the distro's 21:
$ sudo update-alternatives --install /usr/bin/java java /opt/lab/jdk-17.0.17+10/bin/java 1700
$ update-alternatives --query java | grep -E '^(Status|Value|Alternative|Priority)'
Status: auto
Value: /usr/lib/jvm/java-21-openjdk-amd64/bin/java
Alternative: /opt/lab/jdk-17.0.17+10/bin/java
Priority: 1700
Alternative: /usr/lib/jvm/java-21-openjdk-amd64/bin/java
Priority: 2111
Installed, registered, and ignored. In auto mode the highest priority wins, and Ubuntu gives its OpenJDK 21 package 2111.
Teardown: rm -rf ~/labbin, then sudo update-alternatives --remove java /opt/lab/jdk-17.0.17+10/bin/java.
First match wins
Your shell walks the folders in PATH from left to right and runs the first java it finds. Your JDK is on the list. Something else is ahead of it, or the "something else" is a link pointing at an old JDK, or the shell cached an old answer. Almost always the fix is order. Now and then it's a stale link or cache on top.
Three places the order gets decided
What I checked for this one: bash 5.2.21's lookup and hash behaviour, Debian's alternatives in auto and manual mode, and how java -version, which, type and java.home disagree with each other. The Windows and macOS behaviour below comes from Microsoft's and Apple's documentation, not from my own runs.
Linux: the alternatives priority. /usr/bin/java points at /etc/alternatives/java, which points at a real JDK. In auto mode, installing a package with a higher priority quietly moves that link. That's the classic "I ran apt upgrade and now java is 25" surprise. Switching by hand with --config or --set flips the group to manual mode, and from then on package installs leave it alone. Which one you're in is the Status: line above.
Windows: system before user. This is the one people fight for an hour. The effective Path a new process gets is the system Path followed by the user Path (Raymond Chen walks through how the two are combined on The Old New Thing). So if Oracle's installer put C:\Program Files\Common Files\Oracle\Java\javapath in the system Path, and you added %JAVA_HOME%\bin to your user Path, you lose. Every time. Moving your entry to the top of the user list does nothing, because the whole user list sits after the system one. The surprise for me was how often the javapath folder belongs to a JDK that isn't even the newest one installed: per JDK-8259858, those shortcuts don't always follow later installs.
macOS: a stub that reads JAVA_HOME. /usr/bin/java on a Mac isn't a JDK. It's a stub that, per Apple's java_home man page, uses JAVA_HOME if you've set it and otherwise picks the newest JDK in /Library/Java/JavaVirtualMachines. So on a Mac a stale JAVA_HOME in ~/.zprofile does decide which java runs, the opposite of Linux. And Homebrew's openjdk is keg-only: it isn't linked into /opt/homebrew/bin and isn't seen by the stub until you add the symlink Homebrew's install notes tell you to.
Fixing the wrong Java version on each OS
The order of operations is the same everywhere: list every match, decide which one should win, make it win, then prove it in a new shell.
Linux
If you installed a JDK from a package, let alternatives own java:
sudo update-alternatives --config java # pick the JDK; this sets manual mode
sudo update-alternatives --config javac # separate group, keep them matched
readlink -f /usr/bin/java # confirm the real target
Fedora and RHEL use alternatives with the same arguments. If /usr/bin/java points at a JDK you deleted, --config won't list it; sudo update-alternatives --remove java <dead-path> cleans up the broken entry first.
If you unpacked a tarball into /opt or your home folder, prepend it, don't append it:
# ~/.bashrc or ~/.zshrc
export JAVA_HOME=/opt/jdk-17
export PATH="$JAVA_HOME/bin:$PATH" # first, not last
Then hash -r (bash) or rehash (zsh) in any shell that's already open.
macOS
/usr/libexec/java_home -V # every JDK the system knows about
export JAVA_HOME=$(/usr/libexec/java_home -v 17)
export PATH="$JAVA_HOME/bin:$PATH"
Put both exports in ~/.zprofile and grep your other startup files for an older JAVA_HOME line that runs after yours. Last assignment wins.
Windows
- Open System Properties, Environment Variables, and look at the system
Pathfirst. - If
...\Common Files\Oracle\Java\javapathor an old...\jre1.8.0_xxx\binis there, either remove it or move%JAVA_HOME%\bininto the systemPathabove it. Your userPathcan't outrank a system entry. - Uninstalling an old Java 8 JRE from Apps removes its
javapathentry for you, which is cleaner than editing by hand. - Close every terminal and your IDE, open a new one, and run
where.exe java. The first line is the winner.
Things that look like fixes
Reinstalling the JDK changes nothing if the old one is still earlier on PATH. Setting JAVA_HOME on Linux or Windows doesn't change which java your shell runs. And deleting /usr/bin/java by hand breaks the alternatives system for the next package install. Use --remove or --config.
One owner for /usr/bin/java
The setups that go wrong are the ones where three tools each think they own java: a distro package, a tarball in /opt added to PATH, an IDE-downloaded JDK, and the version manager you tried once in 2023. Pick one owner per machine. On a Linux server that's alternatives. On a laptop where you switch versions all day, a version manager like SDKMAN! owns a single PATH entry (~/.sdkman/candidates/java/current/bin) and swaps what it points to, so the order never changes again. On Windows, one %JAVA_HOME%\bin entry in the system Path, no vendor javapath, and you switch by changing JAVA_HOME alone.
For builds, don't depend on whichever java the shell found at all. Maven Toolchains or Gradle's Java toolchains name the version in the build file, so a laptop with the wrong java first on PATH still compiles our report exporter for 17.
Make the build say which Java it got
The cheapest check there is: make every CI job print its Java before doing anything else, and fail when it's wrong.
java -XshowSettings:properties -version 2>&1 | grep -E 'java.home|java.version '
java -version 2>&1 | grep -q '"17\.' || { echo "expected Java 17"; exit 1; }
For services, take the lookup away entirely: a systemd unit with ExecStart=/opt/jdk-17/bin/java -jar exporter.jar can't pick the wrong java, because nobody searches PATH.
On version safety: none of this changes between JDK 8 and 25. The lookup rules belong to your shell and OS. What does change is the folder name on every update (jdk-17.0.16 to jdk-17.0.17), so point PATH at a stable symlink like /opt/jdk-17, and re-check update-alternatives --query java after any apt or dnf upgrade that pulls in a new JDK package.
Worth remembering
type -a java(orwhere.exe java) shows every candidate. The first line runs.java.homefrom-XshowSettings:propertiesis the only answer that can't lie to you.- On Windows, the system
Pathbeats the userPath, so an oldjavapaththere always wins. - Bash caches command locations.
whichand what actually runs can disagree untilhash -r. - On Linux,
JAVA_HOMEdoesn't pick yourjava. On a Mac, through the/usr/bin/javastub, it does.
What people ask about this
Why does java -version show an old version after installing a new JDK?
Because another folder containing java comes earlier on PATH than your new JDK's bin, or /usr/bin/java still links to the old JDK. Run type -a java (or where.exe java on Windows) to see every match in order, then move your JDK's bin ahead of the others or switch the link with update-alternatives --config java.
How do I fix /usr/bin/java pointing to the wrong JDK?
Check where it really goes with readlink -f /usr/bin/java, then run sudo update-alternatives --config java and pick the JDK you want. That switches the group to manual mode, so later package installs won't flip it back. If the old target was deleted, remove the dead entry with update-alternatives --remove java <path> first.
Oracle javapath overrides my JDK on Windows. How do I fix it?
C:\Program Files\Common Files\Oracle\Java\javapath usually sits in the system Path, and Windows puts the system Path before your user Path. Remove the javapath entry (or uninstall the old Oracle JRE that added it), or put %JAVA_HOME%\bin in the system Path above it, then open a new terminal and confirm with where.exe java.
Does JAVA_HOME change which java runs?
Not on Linux or Windows: your shell finds java through PATH, and JAVA_HOME is read by tools like Maven, Gradle, IDEs and app-server start scripts. On macOS the /usr/bin/java stub does use JAVA_HOME when it's set. To make both agree everywhere, put $JAVA_HOME/bin first on PATH.