Linux Commands

How to Check JDK Version in Linux?

How to Check JDK Version in Linux copy

Java JDK or Java Development Kit consists of a bundle of Java Development tools and libraries useful in creating components and building software applications that solely use Java Programming. Linux Distributions like Ubuntu use the Java Virtual Machine (JVM) for hosting Java Programs and the necessary tools to run and develop these Java Programs are provided by the JDK.

This article explains how you can check the JDK Version in Ubuntu.

How to Check JDK Version in Linux/Ubuntu?

Checking the Java JDK Version in Ubuntu is useful for avoiding any compatibility issues when developing applications with Java. It also helps in troubleshooting bugs in your Java Applications. If the Java JDK is not installed on your Ubuntu System, you can install it by following the How to Install Java on Ubuntu Guide. Multiple methods can be used to check the Java JDK Version through Terminal and each method is explained below.

Open Terminal

As you have to check the Java version through the Ubuntu Terminal, open the Terminal Directly with the shortcut key “ctrl+alt+T”:

Additionally, the Terminal can be opened by navigating through the GUI. Click on the “Menu Icon” at the bottom-left of the screen:

Once the Application Menu appears, you will see the Terminal Application Icon:

Click on it to fire up your Ubuntu Terminal:

Method 1: Using the Java Version Command

The Java Version can be directly checked with the “-version” command. The “-version” command allows us to check the currently installed version of any package in our Ubuntu System. In our case, we run the command below to check the Java JDK version installed on our Ubuntu System:

java -version

The currently installed version will be displayed in the Terminal, in our case, the “openjdk version 11” is installed:

Method 2: Using Update-Alternative List to Find Version

The “update-alternatives” maintain and display information about the files on your Ubuntu System. It can be used to display information about Java:

update-alternatives --list java

This will list the installed Java Packages and you can see that Java JDK Version 11 is installed on our Ubuntu System:

Method 3: Searching in the Installed Package List

The Java JDK version can also be checked by searching for the installed Java packages in the Installed Packages List. Use the “list” command to list all the installed packages on your Ubuntu System:

sudo apt list --installed

All the packages that are installed will be listed in the Terminal:

Scroll down through the list and look for the “OpenJDK” version of Java:

The installed JDK Version can be seen in the List, in our case it is JDK Version 11:

Instead of Scrolling Down and looking for the Installed Java Packages, filter your search with the “grep” command. Use the “grep” command to list only the packages having the “openjdk” string:

sudo apt list --installed | grep -i openjdk

The Terminal will display only the “openjdk” packages and in our case, you can see that JDK Version 11 is installed:

Bonus Method: Upgrading Java to the Latest Version

Upgrading the Java JDK version to the newest version improves the efficiency of the Ubuntu System by improving the bugs, improving compatibility, and the overall stability of the system. Upgrading to the latest version also helps in the overall security of the system by protecting applications from any vulnerabilities. The steps below explain how you can upgrade to the latest Java JDK Version.

Download Archive File

To install the latest Java JDK Version on your Ubuntu System, download the “tar.gz” Archive File from the official Java Repository with the “wget” command. The latest Java JDK Version is “Open JDK Version 12”. Download the “tar.gz” file with the following command:

wget https://download.java.net/java/GA/jdk12.0.2/e482c34c86bd4bf8b56c0b35558996b9/10/GPL/openjdk-12.0.2_linux-x64_bin.tar.gz

The downloading process will start and can be seen in the Terminal. Wait for the File to be completely downloaded:

The cursor moves to the next line once the Archive File is Downloaded:

Create a Java Directory

Create a Directory where the Java JDK Packages will be installed. Inside your “/usr” path, create a Java Directory with the “mkdir” command:

sudo mkdir java

The “ls” command lists the files and folders in the directory and the “java” directory can be seen inside the “/usr” directory:

Move the Java JDK Archive to the Java Directory

Move the Archive File to the newly created java directory with the “mv” command:

sudo mv openjdk-12.0.2_linux-x64_bin.tar.gz /usr/java

The mouse cursor will move to the next line once the file is moved to the “java” directory:

Extract Archive

Navigate to the “usr/java” directory with the “cd” command. Once you are in the “java” directory, use the “tar” command to unpack or extract the “tar.gz” file:

sudo tar -xzvf openjdk-12.0.2_linux-x64_bin.tar.gz

The “tar” command will start extracting the files:

Set Environment Variables

Once the files are extracted, set the environment variables to point programs to that version of Java JDK. To set the Environment Variables for the new Java Version, open the “profile” file from the “/etc” directory using the “nano” Command:

sudo nano /etc/profile

The “profile” file will open with the default Text Editor:

Once the file is opened, add the code below inside the file:

JAVA_HOME=/usr/java/jdk-12.0.2
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH

Press the “ctrl+o” keys to write the code to the “profile” file:

Press the “ctrl+x” keys to close the Text Editor and return to the Terminal.

Configure Java

Ubuntu by Default will use the Java JDK Version 11. Configure Java to use the latest JDK version using the command:

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/java/jdk-12.0.2/bin/java" 1

The cursor moves to the next line indicating that the new Java JDK Alternative is installed in the “/usr/bin/java” path whereas the “1” sets the new alternative to run as default:

Similarly, install the new Java JDK Alternative in the “usr/bin/javac” path and set the priority to “1”:

sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/java/jdk-12.0.2/bin/javac" 1

The cursor moves to the next line indicating the new Java JDK alternative is installed in the “/usr/bin/javac” path and it is set as default with the priority “1”:

Verify Installation

Once the new JDK Version is installed and set to default, verify the installation and the configurations with the “-version” command:

java -version

If any previous version of Java was installed, use the following command to check the version:

sudo update-alternatives --config java

In our case, Java was installed previously, and by running the “–config java” command we will get the list of installed versions with their path and priority:

If you want to keep the default selected version, press “enter”, otherwise type the selection number to use that version as default. In our case, we have selected “1” to be the default version as it is the newest Java JDK Version installed:

Once the default version of Java JDK is updated, the Terminal will display the path of the Java being used currently. In our case, Java JDK version 12 is currently being used as default:

Now, running the Java Version command again, the latest version of Java JDK (in our case JDK Version 12) is used:

Conclusion

The Java JDK Version in Ubuntu can be checked by either using the “-version” command, the “update-alternative” list, or by searching for the OpenJDK Packages in the Package Manager. All the methods to check the JDK Version are explained in depth in this article.

Similar Posts