How to Check Java Version in Linux
-
Use the
version
Command to Check Java Version in Linux - Display Path to Check Java Version in Linux
-
Use the
whereis
Command to Check the Java Version in Linux - Conclusion
In this article, we shall learn to check the Java version on our Linux machine. We would additionally see the method to update the Java version.
Use the version
Command to Check Java Version in Linux
Linux provides us with several different commands that we can execute from the terminal. One such command is the version
command.
The man
page of the version
command displays the OpenSSL version number of software installed on Linux.
We will follow the below steps on a Linux machine to get the version number of Java.
-
Open the Linux terminal.
-
Execute the following command.
java - version
-
If Java is installed on your machine, it will show the OpenJDK version.
Let us see a working example to check the version of Java.
stark@stark:~$ java -version
openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.18.04)
OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.18.04, mixed mode, sharing)
To read the Linux manual page of the version command, you can execute the man
command in the Linux terminal.
man version
This command will display all the information related to the version command.
Display Path to Check Java Version in Linux
Other than the method given above, we can try to display the path of where Java is installed. In this way, we will get the version of Java because the path is shown with a full package name that includes the version information.
To check the version using this method, execute the following command in your Linux terminal.
update-alternatives --list java
Output:
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
Note that the output can vary depending on your machine and Java version.
Use the whereis
Command to Check the Java Version in Linux
We can also use the whereis
Linux command to check the version of Java installed on our machine. However, we will not get the version of Java directly using this command.
The whereis
command lists the path of Java installed on the machine. We can then list all the directory contents recursively until we get the Java package listed as an item.
We can then check the version number, a suffix of the package name.
Let us understand this method with the help of an example.
stark@stark:~$ whereis java
java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz
We will then list the contents of '/usr/bin/java'
by executing the ls
command.
stark@stark:~$ ls -l /usr/bin/java
lrwxrwxrwx 1 root root 22 Apr 28 2019 /usr/bin/java -> /etc/alternatives/java
You should note that -l
is a flag that denotes the long-listed format for contents.
Further, we will list all contents of '/etc/alternatives/java'
using the ls
command.
stark@stark:~$ ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 43 Apr 28 2019 /etc/alternatives/java -> /usr/lib/jvm/java-11-openjdk-amd64/bin/java
You can see that we get 'java-11-openjdk-amd64'
in the output. Thus, we got the version of Java installed on our machine.
Conclusion
We have seen three different methods to check the version of Java installed on our machine. You can use one of the methods as per your needs.
Apart from the methods given in this article, you can use third-party libraries such as yum
or rpm
to check the version of Java.
However, these libraries have a drawback: we should give them the package’s exact name to know the version or path information, making them difficult to use.