How to Check Java Version

By 

Updated on

4 min read

Check Java Version

Java is one of the most popular programming languages in the world, used for building different types of cross-platform applications.

This article explains how to check what version of Java is installed on your Linux system using the command line. This can be useful when installing applications that require a specific version of Java.

Java Versioning

Java uses semantic versioning . Production-ready releases are versioned in the following scheme:

txt
MAJOR.MINOR.SECURITY

For example, in Java 21.0.4, 21 is a major version, 0 is a minor version, and 4 is a security version.

  • MAJOR - Major releases bring new features and functions.
  • MINOR - Minor releases contain various bug fixes and compatible improvements.
  • SECURITY - Security releases provide critical security fixes.

Oracle publishes a new major version every six months. Long-term support (LTS) releases, which receive updates for several years, come out every two years. The current LTS versions are Java 17, 21, and 25.

Checking the Java Runtime Version

To find out which Java version is installed on your system, run the java -version command:

Terminal
java -version

The command prints the default Java runtime version:

output
openjdk version "21.0.4" 2024-07-16
OpenJDK Runtime Environment (build 21.0.4+7-Ubuntu-1ubuntu224.04)
OpenJDK 64-Bit Server VM (build 21.0.4+7-Ubuntu-1ubuntu224.04, mixed mode, sharing)

In this example, Java version 21.0.4 is installed. The version on your system may differ depending on which package you installed.

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:

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:

Terminal
javac -version

The output shows the compiler version:

output
javac 21.0.4

If the command is not found, only the JRE is installed. 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.

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:

Terminal
echo $JAVA_HOME

If the variable is set, the output shows the installation path:

output
/usr/lib/jvm/java-21-openjdk-amd64

An empty output means JAVA_HOME is not configured. You can set it by adding the following line to your ~/.bashrc or ~/.profile file:

sh
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64

Replace the path with the actual location of your Java installation. To find the correct path, run:

Terminal
sudo update-alternatives --list java

The listed path typically ends with /bin/java. Remove that suffix to get the value for JAVA_HOME.

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:

Terminal
sudo update-alternatives --config java

If you have only one Java installation, the output looks like this:

output
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:

output
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:

Terminal
java -version 2>&1 | head -n 1 | awk -F '"' '{print $2}'

This returns a string such as 21.0.4 that you can compare in your script.

Conclusion

Use java -version to check the installed runtime and javac -version to verify the JDK. If you need to manage multiple versions, update-alternatives lets you switch the default with a single command.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

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