How to Check Java Version on Linux?
Direct Answer:
To check the Java version on Linux, you can use the following command in your terminal:
java -version
This command will display the version of Java currently installed on your system.
Detailed Instructions
Method 1: Using the Command Line (java -version)
The simplest way to check the Java version on Linux is to use the java -version command. This command is available by default in most Linux distributions, and it provides information about the Java version, vendor, and the Java Runtime Environment (JRE) version.
Here are the steps:
- Open a terminal: Open a terminal emulator, such as Gnome Terminal or Konsole, on your Linux system.
- Type
java -versionand press Enter: Type the commandjava -versionand press Enter. This will execute the command and display the Java version information.
Example Output:
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-8u242-b08-0-b72-b01)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)
Method 2: Using the javac Command
Another way to check the Java version is to use the javac command, which is used to compile Java source files. While this command is not specifically designed to display Java version information, it can be used to check the Java version:
- Open a terminal: Open a terminal emulator, such as Gnome Terminal or Konsole, on your Linux system.
- Type
javac -versionand press Enter: Type the commandjavac -versionand press Enter. This will display the Java compiler version information.
Example Output:
javac 1.8.0
Java 1.8.0
Method 3: Using the update-alternatives Command (For Ubuntu-based Systems)
If you are using an Ubuntu-based Linux system, you can use the update-alternatives command to list all installed Java versions and choose the one you want to use:
- Open a terminal: Open a terminal emulator, such as Gnome Terminal or Konsole, on your Linux system.
- Type
update-alternatives --list javaand press Enter: Type the commandupdate-alternatives --list javaand press Enter. This will display a list of installed Java versions.
Example Output:
java-1.8.0-openjdk
java-1.7.0-openjdk
Table: Comparison of Java Versions on Linux
| Version | Description | End-of-Life |
|---|---|---|
| 1.6 | End of Life (2013) | Yes |
| 1.7 | End of Life (2022) | Yes |
| 1.8 | Current Stable Release | No |
| 1.9 | Preview Release (Not Production-ready) | No |
Common Issues and Solutions
- Error:
java -versioncommand not found: This error occurs when the Java Development Kit (JDK) is not installed on your system. Download and install the JDK from the Oracle website or your Linux distribution’s package manager. - Error:
java -versioncommand returns an old version: This error occurs when you have multiple Java versions installed on your system. You can use theupdate-alternativescommand to switch to a newer version.
Conclusion
In this article, we have discussed three methods to check the Java version on Linux: using the java -version command, using the javac command, and using the update-alternatives command (for Ubuntu-based systems). We have also covered common issues and solutions related to checking Java version on Linux. By following these methods, you can determine the version of Java installed on your Linux system and troubleshoot any issues related to Java. Remember to keep your Java version up-to-date to ensure the best performance and security for your applications.
