To check Java version on a Linux machine, simply execute `java -version` in the command line. This is the fastest way to see what Java runtime is installed on your system. Knowing how to check java version linux is essential for developers and system administrators who rely on Java for applications.
Java is a core component for many Linux applications. Whether you are running a web server, a development tool, or a custom program, you need the correct version. This guide walks you through every method to verify your Java installation on Linux.
Why You Need To Check Java Version On Linux
Java updates frequently. New versions bring security patches and performance improvements. If you run outdated Java, your system might be vulnerable. Also, some software requires a specific Java version. Checking your version helps avoid compatibility issues.
You might also need to check Java for troubleshooting. Error messages often point to version mismatches. Knowing your current version saves time during debugging.
How To Check Java Version Linux Using Terminal
The terminal is the most common way to check Java on Linux. Open your terminal emulator. You can find it in your applications menu or use a shortcut like Ctrl+Alt+T.
Using The Java Version Command
Type the following command and press Enter:
java -version
This command prints the Java version to the terminal. You will see output similar to this:
openjdk version "11.0.20" 2023-07-18 LTS
OpenJDK Runtime Environment (build 11.0.20+8-post-Ubuntu-1ubuntu122.04)
OpenJDK 64-Bit Server VM (build 11.0.20+8-post-Ubuntu-1ubuntu122.04, mixed mode, sharing)
The first line shows the version number. In this example, it is Java 11. The second line shows the runtime environment. The third line shows the virtual machine details.
What If The Command Is Not Found?
If you see an error like “command not found,” Java is not installed. You can install it using your package manager. For Ubuntu or Debian, use:
sudo apt update
sudo apt install default-jdk
For Fedora or CentOS, use:
sudo dnf install java-11-openjdk
After installation, run `java -version` again.
Checking The Java Compiler Version
If you have the Java Development Kit (JDK) installed, you can also check the compiler version:
javac -version
This shows the version of the Java compiler. It should match your runtime version. If they differ, you might have multiple Java installations.
How To Check Java Version Linux Using Package Manager
Your package manager can show installed Java packages. This method is useful if you want to see all Java-related packages on your system.
On Debian Or Ubuntu Systems
Use the dpkg command:
dpkg -l | grep java
This lists all installed packages with “java” in their name. You will see the version number in the output. For example:
ii default-jdk 2:1.11-72build2 amd64 Standard Java or Java compatible Development Kit
ii openjdk-11-jdk 11.0.20+8-1ubuntu1 amd64 OpenJDK Development Kit (JDK)
On Red Hat Or Fedora Systems
Use the rpm command:
rpm -qa | grep java
This lists all installed RPM packages containing “java”. You can see the exact version and release number.
On Arch Linux Systems
Use pacman:
pacman -Q | grep java
This shows installed Java packages from the Arch repositories.
How To Check Java Version Linux Using Environment Variables
Java uses environment variables to locate its installation. You can check these variables to find the version.
Checking JAVA_HOME
The JAVA_HOME variable points to the Java installation directory. Run:
echo $JAVA_HOME
If this variable is set, it shows the path. You can then check the version from that path:
$JAVA_HOME/bin/java -version
Checking PATH
The PATH variable includes the directory of the Java executable. Run:
which java
This shows the location of the java command. For example:
/usr/bin/java
You can then run:
/usr/bin/java -version
How To Check Java Version Linux With Multiple Installations
Many Linux systems have multiple Java versions installed. This is common for development environments. You need to know which version is active.
Using The Update-Alternatives System
Linux uses the update-alternatives system to manage multiple versions. Run:
update-alternatives --config java
This shows a list of installed Java versions. The current selection is marked with an asterisk. You can switch versions by entering the number.
Example output:
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-8-openjdk-amd64/jre/bin/java 1081 manual mode
2 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
Checking All Installed Java Versions
You can list all Java installations in the common directory:
ls /usr/lib/jvm/
This shows directories for each Java version. For example:
java-1.11.0-openjdk-amd64
java-1.8.0-openjdk-amd64
java-11-openjdk-amd64
java-8-openjdk-amd64
You can then check each version by specifying the full path:
/usr/lib/jvm/java-8-openjdk-amd64/bin/java -version
How To Check Java Version Linux Using GUI
If you prefer a graphical interface, some Linux distributions include a Java control panel. This is less common on servers but available on desktop systems.
On Ubuntu With GNOME
Search for “About Java” in the applications menu. This opens the Java control panel. The version is displayed on the General tab.
Using System Settings
Some desktop environments show Java information in system settings. Look for “Java” or “Java Runtime Environment” in the settings menu.
How To Check Java Version Linux For Specific Applications
Sometimes you need to check the Java version used by a specific application. This is common for web servers like Tomcat or Jenkins.
Checking Java Version For Tomcat
Tomcat uses the JAVA_HOME variable. You can check the Tomcat startup script or the catalina.sh file:
grep JAVA_HOME /usr/share/tomcat/bin/catalina.sh
This shows the Java path used by Tomcat. You can then check that path’s version.
Checking Java Version For Jenkins
Jenkins runs as a service. Check the service configuration:
systemctl cat jenkins | grep JAVA
This shows the Java options used by Jenkins. You can also check the process:
ps aux | grep java
This shows the full command line, including the Java version used.
Common Issues When Checking Java Version
You might encounter problems when running the version command. Here are common issues and fixes.
Java Command Not Found
This means Java is not installed or not in your PATH. Install Java using your package manager. If Java is installed but not in PATH, you can add it:
export PATH=$PATH:/usr/lib/jvm/java-11-openjdk-amd64/bin
Add this line to your ~/.bashrc file to make it permanent.
Wrong Version Displayed
If the version shown is not what you expect, you might have multiple installations. Use the update-alternatives command to switch versions. Also check your JAVA_HOME variable.
Permission Denied
If you get a permission error, you might need to run the command with sudo. However, for checking the version, you usually do not need root access. If the Java binary is not executable, check its permissions:
ls -l /usr/bin/java
It should be executable. If not, use chmod to fix it.
How To Check Java Version Linux In Scripts
You can automate version checking in shell scripts. This is useful for deployment or CI/CD pipelines.
Simple Version Check Script
Create a script file:
#!/bin/bash
java_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2)
echo "Java version is: $java_version"
This extracts the version number from the output. You can then use it in conditional statements.
Checking For Minimum Version
You can check if the installed Java meets a minimum requirement:
#!/bin/bash
required_version="11"
java_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2 | cut -d'.' -f1)
if [ "$java_version" -ge "$required_version" ]; then
echo "Java version $java_version meets requirement."
else
echo "Java version $java_version is too old. Need version $required_version or higher."
fi
How To Check Java Version Linux On Different Distributions
While the basic command is the same, some distributions have slight differences. Here are specific notes for popular distributions.
On Ubuntu And Debian
These distributions use APT. The default Java is often OpenJDK. Use `java -version` as usual. You can also check with `apt list –installed | grep java`.
On Fedora And CentOS
These use DNF or YUM. The command is the same. You can check with `dnf list installed | grep java` or `yum list installed | grep java`.
On Arch Linux
Arch uses Pacman. The default Java is also OpenJDK. Use `java -version` or `pacman -Qi jre-openjdk` for detailed information.
On OpenSUSE
OpenSUSE uses Zypper. The command is the same. Use `zypper se –installed-only java` to list installed Java packages.
How To Check Java Version Linux Without Typing Commands
If you are on a headless server or prefer minimal typing, you can use aliases or scripts.
Creating An Alias
Add this to your ~/.bashrc file:
alias javav='java -version'
Then you can just type `javav` to check the version.
Using A One-Liner Script
You can create a short script in your PATH. Save this as `/usr/local/bin/javaversion`:
#!/bin/bash
java -version 2>&1 | head -n 1
Make it executable with `chmod +x /usr/local/bin/javaversion`. Then run `javaversion` anytime.
How To Check Java Version Linux For Docker Containers
If you use Docker, you need to check Java inside containers. This is important for containerized applications.
Checking Java In A Running Container
Use the docker exec command:
docker exec <container_name> java -version
Replace <container_name> with your container’s name or ID.
Checking Java In A Docker Image
You can check the Java version in an image without running it:
docker run --rm <image_name> java -version
This creates a temporary container and runs the command.
How To Check Java Version Linux For Remote Systems
You can check Java on remote Linux systems using SSH. This is common for managing multiple servers.
Using SSH Command
Run:
ssh user@remote_host java -version
This executes the command on the remote system and shows the output.
Using Ansible
If you use Ansible for configuration management, you can check Java version with an ad-hoc command:
ansible all -m shell -a "java -version"
This checks Java on all hosts in your inventory.
How To Check Java Version Linux For Different Java Implementations
There are multiple Java implementations. The most common are OpenJDK and Oracle JDK. The version command works for both, but the output differs slightly.
OpenJDK Output
OpenJDK shows “openjdk version” in the first line. It is open source and included in most Linux distributions.
Oracle JDK Output
Oracle JDK shows “java version” without the “openjdk” prefix. It is the official Oracle implementation. You might need to download it separately.
Other Implementations
IBM Java, Amazon Corretto, and Azul Zulu are other options. They all respond to the `java -version` command with their own branding.
How To Check Java Version Linux And Update It
After checking your version, you might need to update Java. Here is how to do it on common distributions.
Updating On Ubuntu Or Debian
Use APT:
sudo apt update
sudo apt upgrade default-jdk
This updates to the latest version in the repository.
Updating On Fedora Or CentOS
Use DNF:
sudo dnf update java-*-openjdk
Installing A Specific Version
If you need a specific version, you can install it directly. For example, to install Java 17 on Ubuntu:
sudo apt install openjdk-17-jdk
Then use update-alternatives to set it as default.
Frequently Asked Questions
How Do I Check Java Version In Linux Terminal?
Open your terminal and type `java -version`. This shows the installed Java runtime version.
What If Java -Version Doesn’t Work?
Java might not be installed. Install it using your package manager. For Ubuntu, use `sudo apt install default-jdk`. For Fedora, use `sudo dnf install java-11-openjdk`.
How Can I Check Java Version Without Typing The Full Command?
You can create an alias in your ~/.bashrc file. Add `alias javav=’java -version’` and then type `javav`.
How Do I Check Which Java Version Is Used By A Specific Application?
Check the application’s configuration files or startup scripts. Look for JAVA_HOME or the Java path. You can also check the running process with `ps aux | grep java`.
Can I Check Java Version On A Remote Linux Server?
Yes, use SSH: `ssh user@remote_host java -version`. This runs the command on the remote server.
Conclusion
Checking the Java version on Linux is straightforward. The `java -version` command works on all distributions. You can also use package managers, environment variables, or GUI tools. Knowing your Java version helps with compatibility,