How to Check Java Version on Linux

On Linux, java -version is the universal check. To see every installed JDK and manage which is active, use update-alternatives (Debian/Ubuntu) or alternatives (Fedora/RHEL).

Basic check

java -version

If it says "command not found", Java is not installed or not on PATH. See the full fix guide.

Check the compiler

javac -version

If java -version works but javac fails, you have a JRE, not a JDK. Install the JDK package for your distro.

Find the actual binary (through symlinks)

readlink -f $(which java)

This resolves all symlinks and shows the real path, e.g. /usr/lib/jvm/java-21-openjdk-amd64/bin/java.

List all installed JDKs

Debian/Ubuntu:

update-alternatives --display java
# or list candidates only:
update-alternatives --list java

Fedora/RHEL:

alternatives --display java
ls /usr/lib/jvm/

Arch:

archlinux-java status

Switch the active Java version

# Debian/Ubuntu:
sudo update-alternatives --config java
# Pick a number from the list

# Fedora/RHEL:
sudo alternatives --config java

# Arch:
sudo archlinux-java set java-21-openjdk

Check JAVA_HOME

echo $JAVA_HOME

To set it dynamically from the active Java:

export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
echo $JAVA_HOME

Add that line to ~/.bashrc or ~/.zshrc to persist across sessions.

Check Java version inside a container

docker run --rm eclipse-temurin:21 java -version

Useful when the container's Java differs from the host's Java.