Check Java Version Command in Linux

In software development, Java is a widely used programming language. It’s crucial to know the version of Java installed on your Linux system for compatibility and debugging purposes. This guide will walk you through the process of checking your Java version using the command line in Linux. We’ll also delve into some common issues you might encounter and how to resolve them.

Understanding Java

What is Java?

Java is a high-level, class-based, object-oriented programming language designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let application developers write once run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without recompilation.

Why is Java Version Important?

Knowing the version of Java installed on your system is crucial for several reasons. It helps debug issues related to the Java Runtime Environment (JRE). It also aids in ensuring compatibility between your system and the software you’re trying to run. Some applications require a specific version of Java to function correctly, and knowing your Java version can save you from potential headaches.

Checking Java Version in Linux

Step 1: Open the Terminal

Start by opening the terminal. If you’re on a server, you’re already there. If you’re using Linux, find the ‘Terminal’ in your applications menu or taskbar and open it. Don’t worry about which terminal you use, as most of our commands work in most of them.

Step 2: Enter the Java Version Command

Once you have the terminal open, you can check the Java version by entering the following command:

java -version

This command tells Java to output its version information. If Java is installed on your system, you should see output similar to the following:

java version "17.0.7" 2023-04-18 LTS
Java(TM) SE Runtime Environment (build 17.0.7+8-LTS-224)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.7+8-LTS-224, mixed mode, sharing)

In this example, we’re showing Java version 17.0.7. Your system might show a different version, depending on your installation.

Step 3: Understanding the Output

The output of the java -version command provides several pieces of information:

  • java version "17.0.7" 2023-04-18 LTS: This is the version of Java installed on your system. The “LTS” indicates that this is a Long-Term Support version, meaning it will receive updates and security fixes for longer than non-LTS versions.
  • Java(TM) SE Runtime Environment (build 17.0.7+8-LTS-224): This is the runtime environment, the software layer on which your Java programs run.
  • Java HotSpot(TM) 64-Bit Server VM (build 17.0.7+8-LTS-224, mixed mode, sharing): This is the Java Virtual Machine (JVM), the platform that executes Java bytecode. The “mixed mode” indicates that the JVM is using both interpreted and compiled code for execution, while “sharing” means it’s using class data sharing to improve startup and footprint.

Troubleshooting Java Version Issues

Java Command Not Found

If you enter the java -version command and receive a message saying java: command not found, this means that Java is not installed on your system or the Java executable is not in your system’s PATH.

To resolve this issue, you can install Java using the package manager for your specific Linux distribution. Here’s how to do it on various distributions:

Debian/Ubuntu

sudo apt install default-jdk

Arch Linux

sudo pacman -S jdk-openjdk

Fedora

sudo dnf install java-latest-openjdk

RHEL/CentOS

sudo yum install java-latest-openjdk
sudo dnf install java-latest-openjdk

Gentoo

emerge --ask dev-java/openjdk

openSUSE

zypper install java-{version}-openjdk

Multiple Java Versions Installed

Sometimes, you might have multiple versions of Java installed on your system. You can check this by entering the following command:

update-alternatives --config java

This command lists all the Java versions installed on your system and allows you to choose which one to use by default. The output will look something like this:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java     1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java     1111      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java  1081      manual mode

In this example, two versions of Java are installed: Java 11 and Java 8. The asterisk (*) indicates the default version. You can change the default version by entering the selection number of the version you want.

Conclusion

Knowing how to check your Java version in Linux is fundamental for any software developer or system administrator. It helps ensure compatibility between your system and the software you’re running, aids debugging and allows you to manage multiple Java versions if necessary. By following this guide, you can now check your Java version and troubleshoot common issues.