How to Check Java Version

Before installing an application that runs on Java, such as Tomcat, Jenkins, or Maven, you need to know whether Java is installed on your system and which version it is. Many applications require a specific minimum version and refuse to start with older releases.
This guide explains how to check the installed Java version from the Linux command line, verify the JDK compiler, and find out which installation is the default when multiple versions are present.
Checking the Java Runtime Version
To find out which Java version is installed on your system, run the java -version command:
java -versionThe command prints the default Java runtime version:
openjdk version "21.0.11" 2026-04-21
OpenJDK Runtime Environment (build 21.0.11+10-Ubuntu-1ubuntu124.04.2)
OpenJDK 64-Bit Server VM (build 21.0.11+10-Ubuntu-1ubuntu124.04.2, mixed mode, sharing)In this example, Java version 21.0.11 is installed. The version on your system may differ depending on which package you installed.
Java 9 and newer also accept java --version (with two dashes), which prints the same information to standard output instead of standard error. This distinction matters when you pipe the output to another command.
If you get “java: command not found”, it means that Java is not installed on the system. To install it, use one of the following guides depending on your Linux distribution:
- Install Java on Ubuntu 26.04
- Install Java on Ubuntu 24.04
- Install Java on Ubuntu 22.04
- Install Java on Debian
- Install Java on CentOS
Understanding Java Version Numbers
Since JDK 10, Java version numbers follow the scheme defined in JEP 322:
FEATURE.INTERIM.UPDATE.PATCHFor example, in Java 21.0.11, 21 is the feature release, 0 is the interim release, and 11 is the update release. The patch component is omitted because its value is zero.
FEATURE- Increases every six months with each feature release that brings new language and platform features.INTERIM- Reserved for releases between feature releases. In practice, it is always 0.UPDATE- Increases with each quarterly update that delivers bug and security fixes.PATCH- Increases only for emergency releases that fix critical issues. It is omitted when its value is zero.
Oracle publishes a new feature release every six months. Long-term support (LTS) releases, which receive updates for several years, come out every two years. Java 8, 11, 17, 21, and 25 are LTS releases.
Checking the Java Compiler Version
The java command shows the runtime (JRE) version. If you need to compile Java code, you also need the Java Development Kit (JDK). To check whether the JDK is installed and which version it provides, run:
javac -versionThe output shows the compiler version:
javac 21.0.11If the command is not found, the JDK may not be installed, or the directory containing javac may not be in your PATH. Install the JDK package to get the compiler. On Ubuntu and Debian, the package is named default-jdk or a version-specific package such as openjdk-21-jdk.
Listing Installed Java Versions
A system can have several Java versions installed side by side. To list all of them on Ubuntu, Debian, and derivatives, run:
update-alternatives --list javaThe command prints the full path of every registered Java executable:
/usr/lib/jvm/java-17-openjdk-amd64/bin/java
/usr/lib/jvm/java-21-openjdk-amd64/bin/javaIn this example, two versions are installed: Java 17 and Java 21. You can also inspect the /usr/lib/jvm directory, where Linux distributions place Java installations. On Fedora, RHEL, and derivatives, use alternatives --display java instead.
Checking the JAVA_HOME Variable
Many Java applications and build tools such as Maven, Gradle, and Tomcat rely on the JAVA_HOME environment variable to locate the Java installation. To check whether it is set, run:
echo $JAVA_HOMEIf the variable is set, the output shows the installation path:
/usr/lib/jvm/java-21-openjdk-amd64An empty output means JAVA_HOME is not configured. You can set it by adding the following line to your ~/.bashrc or ~/.profile file:
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64Replace the path with the actual location of your Java installation. Use one of the paths from the previous section, without the /bin/java suffix.
Managing Multiple Java Versions
Your system may have more than one Java version installed at the same time. To check whether that is the case and to switch between them, run:
sudo update-alternatives --config javaIf you have only one Java installation, the output looks like this:
There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-21-openjdk-amd64/bin/java
Nothing to configure.If multiple versions are installed, the command displays a menu where you can select which version will be the default:
There are 3 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 auto mode
1 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode
2 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 manual mode
Press <enter> to keep the current choice[*], or type selection number:Enter the number from the Selection column and press Enter to change the default. This command is available on Debian-based distributions (Ubuntu, Debian, Linux Mint). On Fedora and RHEL-based systems, use alternatives --config java instead.
FAQ
Does java -version show the JDK or the JRE?
It shows whichever runtime is set as the default. Both the JRE-only package and the full JDK include the java command. To confirm that the JDK is installed, check for javac as described above.
How do I check the Java version inside a shell script?
Parse the output of java -version, which prints to standard error:
java -version 2>&1 | head -n 1 | awk -F '"' '{print $2}'This returns a string such as 21.0.11. Do not compare Java versions as ordinary strings because values such as 9 and 17 sort incorrectly. Use a version-aware comparison such as sort -V when your script needs to enforce a minimum version.
Conclusion
Use java -version to check the installed runtime and javac -version to verify the JDK. If you need to manage multiple versions, our update-alternatives guide
explains how to inspect, switch, and automate the default selection.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page