Java is a high-level, object-oriented, and platform-independent programming language characterized by its portability, security, and robustness.
It uses a virtual machine, JVM, to execute bytecode, which allows Java applications to run on various hardware and operating systems without modification.
Over the years, there has been more and more releases of the Java language each with subsequent changes. Although there does come backward compatibility, some versions are breaking. It is therefore good to kwow which Java version you have installed when compiling your code.
This can prevent you from encountering an error when compiling a code due to missing features that were available in a previous version or are available in a more recent release.
Method - Use Java Command
The first and most common method that you can use to check the installed Java version is using the
java command followed by the
openjdk 19.0.2 2023-01-17 OpenJDK Runtime Environment (build 19.0.2+7-Ubuntu-0ubuntu322.04) OpenJDK 64-Bit Server VM (build 19.0.2+7-Ubuntu-0ubuntu322.04, mixed mode, sharing)
The command should return the installed Java version as shown in the example above, we have JDK 19 installed on Ubuntu.
You can also check the primary Java compiler by using the
javac command as shown:
Method 2 - Using the Installed Path
The second method you can use is to check the path where Java is installed. To find the path to the Java directory, run the command:
update-alternatives --list java
The command above should return the path to the installed Java version as shown:
You can also use the
whereis command as shown:
java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz
Method 3 - Using Package List
The last method that you can use to check the installed Java version, you can use the apt command as shown below:
sudo apt list --installed | grep jdk
This should use the apt command to list the installed packages and pipe the output to
grep which checks for the JDK output as shown:
openjdk-19-jdk-headless/jammy-updates,jammy-security,now 19.0.2+7-0ubuntu3~22.04 arm64 [installed,automatic] openjdk-19-jdk/jammy-updates,jammy-security,now 19.0.2+7-0ubuntu3~22.04 arm64 [installed] openjdk-19-jre-headless/jammy-updates,jammy-security,now 19.0.2+7-0ubuntu3~22.04 arm64 [installed,automatic] openjdk-19-jre/jammy-updates,jammy-security,now 19.0.2+7-0ubuntu3~22.04 arm64 [installed,automatic]
In this output, we can see we have Java version 19 installed.
In this tutorial, we discussed the various methods you can use to check the installed Java version on a Linux system. We recommend the first method as it provides a clear output that works on all Linux systems.