How to Check Java Version in Linux
Checking your Java version on a Linux system is a common task for developers and system administrators. Whether you’re setting up a new environment, troubleshooting software, or ensuring compatibility, knowing which Java version is installed is essential. In this article, I’ll guide you through easy methods to find your Java version on Linux, using commands and environment variables.
You might think this is complicated, but it’s actually straightforward. I’ll explain step-by-step how to check your Java version, including how to verify both the Java Runtime Environment (JRE) and Java Development Kit (JDK). By the end, you’ll be confident in managing Java versions on your Linux machine.
Why Checking Java Version Matters on Linux
Java is a popular programming language and platform used in many applications. Different programs require different Java versions, so it’s important to know what you have installed. Here’s why checking your Java version on Linux is useful:
- Compatibility: Some applications need specific Java versions to run correctly.
- Security: Older Java versions may have vulnerabilities; updating helps keep your system safe.
- Development: Developers need to confirm the JDK version to compile and run Java programs.
- Troubleshooting: Knowing the Java version helps diagnose issues related to Java applications.
Linux users often have multiple Java versions installed, so checking which one is active is crucial. Let’s explore how to do this easily.
How to Check Java Version Using the Terminal
The most common way to check your Java version on Linux is through the terminal. This method works on most Linux distributions like Ubuntu, Fedora, CentOS, and Debian.
Step 1: Open Your Terminal
You can open the terminal by searching for “Terminal” in your applications menu or pressing Ctrl + Alt + T on most Linux desktops.
Step 2: Run the Java Version Command
Type the following command and press Enter:
java -version
This command tells the system to display the installed Java runtime version.
What You’ll See
The output looks something like this:
openjdk version "17.0.5" 2026-04-20
OpenJDK Runtime Environment (build 17.0.5+10)
OpenJDK 64-Bit Server VM (build 17.0.5+10, mixed mode)
This output shows the Java version installed (OpenJDK 17.0.5 in this example). If you see an error like command not found, Java might not be installed or not added to your system’s PATH.
Checking the JDK Version
If you want to check the Java Development Kit (JDK) version, use:
javac -version
This command shows the version of the Java compiler, which is part of the JDK. For example:
javac 17.0.5
If this command fails, it means the JDK is not installed, or the javac command is not in your PATH.
How to Check Java Version Using Environment Variables
Sometimes, Java is installed but not set as the default version. You can check environment variables to see which Java version your system uses.
Step 1: Check JAVA_HOME Variable
The JAVA_HOME environment variable points to the Java installation directory. To check it, run:
echo $JAVA_HOME
If this returns a path like /usr/lib/jvm/java-17-openjdk-amd64, it shows where Java is installed.
Step 2: Verify the Java Binary Path
You can also check which Java binary is used by default with:
which java
This command returns the path to the Java executable, for example:
/usr/bin/java
You can then check the version of this Java binary by running:
/usr/bin/java -version
This helps confirm the exact Java version your system uses when you run Java commands.
Managing Multiple Java Versions on Linux
Linux systems often have multiple Java versions installed. You can switch between them using tools like update-alternatives on Debian-based systems.
Using update-alternatives to Switch Java Versions
- List all installed Java versions:
sudo update-alternatives --config java
- You’ll see a list like this:
There are 3 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
3 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1200 manual mode
Enter the number corresponding to the Java version you want to use and press Enter.
Verify the change by running:
java -version
This method lets you switch Java versions without uninstalling any.
Installing Java on Linux If Not Installed
If your system doesn’t have Java installed, you can easily add it.
Installing OpenJDK on Ubuntu/Debian
sudo apt update
sudo apt install openjdk-17-jdk
Installing OpenJDK on Fedora/CentOS
sudo dnf install java-17-openjdk-devel
After installation, check the version again with java -version.
Troubleshooting Common Issues When Checking Java Version
Sometimes, you might face issues when checking your Java version. Here are common problems and fixes:
- Command not found: Java is not installed or not in PATH. Install Java or add it to PATH.
- Wrong version showing: Multiple Java versions installed; switch using
update-alternatives. - JAVA_HOME not set: Set the variable in your shell profile (
~/.bashrcor~/.zshrc):
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
Reload the profile with source ~/.bashrc.
Summary Table: Commands to Check Java Version on Linux
| Command | Purpose | Example Output |
java -version | Check Java Runtime Environment | openjdk version "17.0.5" |
javac -version | Check Java Development Kit | javac 17.0.5 |
echo $JAVA_HOME | Show Java installation path | /usr/lib/jvm/java-17-openjdk-amd64 |
which java | Show path to Java executable | /usr/bin/java |
sudo update-alternatives --config java | Switch Java versions | List of installed Java versions |
Conclusion
Now you know how to check your Java version on Linux quickly and easily. Whether you use the java -version command or check environment variables like JAVA_HOME, these methods help you understand your Java setup. You can also manage multiple Java versions using update-alternatives to ensure your system runs the right Java for your needs.
Keeping your Java version updated and knowing which one is active is key for security, compatibility, and development. Next time you need to verify Java on your Linux machine, just open your terminal and follow these simple steps. You’ll be ready to handle any Java-related task with confidence.
FAQs
How do I check if Java is installed on Linux?
Run java -version in the terminal. If Java is installed, it shows the version. If not, you’ll get a “command not found” error.
What is the difference between java -version and javac -version?
java -version shows the Java Runtime Environment version, while javac -version shows the Java compiler version, part of the JDK.
How can I switch between multiple Java versions on Linux?
Use sudo update-alternatives --config java to select the active Java version from installed options.
What does the JAVA_HOME environment variable do?
JAVA_HOME points to the directory where Java is installed. It helps programs find Java binaries and libraries.
How do I set the JAVA_HOME variable permanently?
Add export JAVA_HOME=/path/to/java and export PATH=$JAVA_HOME/bin:$PATH to your shell profile file like ~/.bashrc, then run source ~/.bashrc.
