How to Change Java Version on Windows, Mac, Linux

Changing your active Java version means pointing JAVA_HOME and PATH at a different JDK. The method is slightly different per OS, but the concept is the same on all three.

Prerequisites

You must have both JDK versions installed. Install them side by side — each goes into its own directory and they do not conflict.

Windows

Open System Properties > Advanced > Environment Variables:

  1. Under System variables, find JAVA_HOME and edit its value to the new JDK path, e.g. C:\Program Files\Eclipse Adoptium\jdk-21.0.3.9-hotspot.
  2. In the Path variable, make sure %JAVA_HOME%\bin is the first Java entry (remove any hard-coded JDK paths above it).
  3. Open a new terminal and verify: java -version.

For a temporary switch in the current PowerShell session:

$env:JAVA_HOME = "C:\Program Files\Eclipse Adoptium\jdk-17.0.11.9-hotspot"
$env:PATH = "$env:JAVA_HOME\bin;" + $env:PATH
java -version

macOS

# Switch to Java 17 in current shell:
export JAVA_HOME=$(/usr/libexec/java_home -v 17)
java -version

# Switch to Java 21:
export JAVA_HOME=$(/usr/libexec/java_home -v 21)
java -version

Add shell aliases to ~/.zshrc for quick switching:

alias java17='export JAVA_HOME=$(/usr/libexec/java_home -v 17) && echo "Switched to $(java -version 2>&1 | head -1)"'
alias java21='export JAVA_HOME=$(/usr/libexec/java_home -v 21) && echo "Switched to $(java -version 2>&1 | head -1)"'

Linux

# Interactive menu (Debian/Ubuntu):
sudo update-alternatives --config java

# Non-interactive switch to a specific version (example path):
sudo update-alternatives --set java /usr/lib/jvm/java-17-openjdk-amd64/bin/java
sudo update-alternatives --set javac /usr/lib/jvm/java-17-openjdk-amd64/bin/javac

Per-directory switching (all platforms)

Tools like jenv and SDKMAN! read a .java-version or .sdkmanrc file in the project directory and switch automatically:

# SDKMAN!
sdk use java 17.0.11-tem    # switch current shell
sdk default java 21.0.3-tem  # set global default
# In a project directory:
echo "java=17.0.11-tem" > .sdkmanrc
sdk env install

Check the active version

java -version
javac -version
echo $JAVA_HOME