Introduction
Java is an object-oriented programming language used for building platform-independent applications. Knowing how to check the Java version on Linux is important as different applications sometimes require specific Java versions, ensuring compatibility and optimal performance.
Additionally, checking the version helps manage updates and maintain a secure, up-to-date Java environment on your system.
In this tutorial, learn how to check the Java version installed on Linux.
Prerequisites
Note: If you are running Mac or Windows, use this tutorial to check the version of Java.
How to Check Java Version on Linux
There are several ways to check the Java version on Linux. The following text presents three different methods.
Method 1: java -version Command
To check the Java version on Linux, run the following:
java -version
The output displays the Java package version installed on your system. In the example above, OpenJDK version 11.0.24 is installed.
Note: If the output indicates there is no such package on the system, learn how to install Java on Ubuntu
Another way to get the Java version is to check the version of the primary Java compiler, javac. Do it with the following command:
javac -version
Method 2: Check Java Path
There are two ways to find the Java directory path. The first option is to run the following command:
update-alternatives --list java
The system responds with the path where Java is installed. Use the path to check the Java version:
/usr/lib/jvm/java-11-openjdk-amd64/bin/java -version
Alternatively, use the whereis command and follow the symbolic links to find the Java path. To do it, take the following steps:
1. Run the command:
whereis java
The output shows that Java is located in /usr/bin/java.
2. List the /usr/bin/java directory content with the ls command:
ls -l /usr/bin/java
The output shows that /usr/bin/java is a symbolic link for /etc/alternatives/java.
3. List the content of the provided path by running:
ls -l /etc/alternatives/java
The output displays /etc/alternatives/java is another symbolic link and that the actual Java directory path is /usr/lib/jvm/java-11-openjdk-amd64/bin/java.
4. Verify the version with:
<em>/usr/lib/jvm/java-11-openjdk-amd64/bin/java.</em> -version
Method 3: Find Java in Installed Packages
You can also prompt the system to list installed packages and search for Java with its version number.
To generate a list of all installed packages, use the command:
sudo apt list --installed
Scroll up or down until you find the Java packages, as shown in this example.
To avoid searching through all installed packages, list Java packages only. Prompt the system to list Java packages by searching for openjdk with grep:
sudo apt list --installed | grep -i openjdk
Conclusion
This article explained how to check the Java version on Linux using three different methods.
Next, learn how to install Java on different Linux distributions, such as Raspberry Pi and Fedora.