Knowing how to check java version in linux is essential for developers and system administrators. This quick guide shows you the exact commands to find your Java installation details. You’ll learn multiple methods to verify the version, whether you need it for debugging or compatibility checks.
Java version on Linux is revealed by running `java -version`, which prints the version string to standard error. This command works on almost every Linux distribution, including Ubuntu, CentOS, and Fedora. Let’s walk through the steps together.
How To Check Java Version In Linux
The most direct way to check your Java version is using the terminal. Open your terminal emulator by pressing Ctrl+Alt+T or searching for “Terminal” in your applications menu. Once the terminal is open, type the following command and press Enter:
java -version
This command outputs something similar to:
openjdk version "11.0.20" 2023-07-18
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)
Notice that the version number appears in the first line. In this example, it’s Java 11. If you see an error like “java: command not found,” it means Java is not installed or not in your PATH.
You can also check the version of the Java compiler, which is often useful for development. Run:
javac -version
This will show the version of the Java compiler installed. It should match your runtime version if both are from the same installation.
Understanding The Output
The output from `java -version` contains three key pieces of information:
- Version number: The major version (e.g., 11, 17, 21) and update number
- Runtime environment: Whether it’s OpenJDK or Oracle JDK
- VM details: 32-bit vs 64-bit, server or client mode
This information helps you ensure compatibility with your applications. For example, some older software may require Java 8, while newer frameworks need Java 17 or later.
Checking Java Version Without Running Java
Sometimes you might not have Java in your PATH, but you know it’s installed somewhere. You can locate the Java executable and check its version directly. Use the find command to search for the java binary:
find / -name java -type f 2>/dev/null
Once you find the path (e.g., /usr/lib/jvm/java-11-openjdk-amd64/bin/java), run it with the version flag:
/usr/lib/jvm/java-11-openjdk-amd64/bin/java -version
This method works even if Java is not in your default PATH. It’s particularly useful when you have multiple Java installations.
Using Alternative Commands
Beyond the basic `java -version`, there are other commands that reveal Java version details. The java -fullversion command gives a more concise output:
java -fullversion
This prints something like:
java full version "11.0.20+8-post-Ubuntu-1ubuntu122.04"
Another useful command is java -XshowSettings:all, which displays all Java settings, including version info. This is helpful for debugging configuration issues.
Checking Java Version In Scripts
If you’re writing a shell script that needs to check the Java version, you can capture the output and parse it. Here’s a simple example:
#!/bin/bash
java_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2)
echo "Java version: $java_version"
This script extracts the version number from the first line of the `java -version` output. The 2>&1 redirects stderr to stdout because Java prints version info to stderr.
You can also check if a specific version is installed using conditional logic:
if java -version 2>&1 | grep -q "version \"11"; then
echo "Java 11 is installed"
else
echo "Java 11 is not installed"
fi
Multiple Java Versions On Linux
It’s common to have multiple Java versions installed on a single Linux system. You might have Java 8 for legacy apps and Java 17 for new projects. To see all installed Java versions, use the update-alternatives command (on Debian-based systems):
update-alternatives --list java
This shows all Java binaries registered in the alternatives system. On Red Hat-based systems, you can use:
alternatives --list java
To switch between versions, use:
sudo update-alternatives --config java
This presents a menu where you can select the default Java version. After switching, run java -version to confirm the change.
Checking Java Version In Different Distributions
While the core commands are the same across distributions, package managers can help you find installed versions. On Ubuntu or Debian:
dpkg -l | grep openjdk
On CentOS or RHEL:
rpm -qa | grep java
On Fedora:
dnf list installed | grep java
These commands list all installed Java packages, including their version numbers. This is useful when you need to uninstall or update specific versions.
Troubleshooting Common Issues
Sometimes the `java -version` command fails or gives unexpected results. Here are common problems and solutions:
- Command not found: Java is not installed or not in PATH. Install Java using your package manager.
- Wrong version shown: Another Java installation might be taking precedence. Check your PATH variable with
echo $PATH. - Permission denied: The Java binary might not be executable. Use
chmod +x /path/to/javato fix it. - No output: The command might be hanging. Try running with
timeout 5 java -version.
If you see a message like “Error: could not open `…/jvm.cfg`”, it indicates a broken Java installation. Reinstalling Java usually resolves this.
Checking Java Version In Docker Containers
If you’re working inside a Docker container, the same commands apply. First, access the container’s shell:
docker exec -it container_name /bin/bash
Then run java -version as usual. You can also check the version without entering the container:
docker exec container_name java -version
This is useful for automated checks in CI/CD pipelines.
Using Environment Variables
Java version information is also stored in environment variables. The JAVA_HOME variable points to the Java installation directory. To check it:
echo $JAVA_HOME
If this variable is set, you can find the version by looking at the release file inside the directory:
cat $JAVA_HOME/release
This file contains lines like:
JAVA_VERSION="11.0.20"
OS_NAME="Linux"
OS_ARCH="amd64"
This method works even if the java binary is not in your PATH.
Checking Java Version For Different Java Implementations
Besides OpenJDK and Oracle JDK, there are other implementations like IBM Semeru, Amazon Corretto, and GraalVM. The java -version command works for all of them, but the output format may vary slightly. For example, GraalVM might show:
openjdk version "21" 2023-09-19
OpenJDK Runtime Environment GraalVM CE 21.0.0 (build 21+35-jvmci-23.1-b15)
OpenJDK 64-Bit Server VM GraalVM CE 21.0.0 (build 21+35-jvmci-23.1-b15, mixed mode, sharing)
The key is to look for the version number in the first line. The implementation name appears in the second line.
Automating Java Version Checks
For system administrators managing multiple servers, automating Java version checks is efficient. You can write a simple script that runs on all servers via SSH:
#!/bin/bash
for server in server1 server2 server3; do
echo "Checking $server..."
ssh $server "java -version 2>&1 | head -n 1"
done
This script prints the Java version for each server. You can extend it to check for specific versions or send alerts if the version is outdated.
Using Ansible For Java Version Checks
Ansible is a popular automation tool that can check Java versions across many servers. Here’s a simple playbook:
---
- name: Check Java version
hosts: all
tasks:
- name: Get Java version
command: java -version
register: java_version
ignore_errors: yes
- name: Display Java version
debug:
msg: "{{ java_version.stderr_lines[0] }}"
when: java_version.rc == 0
This playbook runs on all hosts and displays the Java version. If Java is not installed, it shows an error message.
Frequently Asked Questions
How Do I Check Java Version In Linux Without Typing The Full Command?
You can use the java -version command as is. If you want a shorter command, you can create an alias in your shell profile: alias jv='java -version'. Then just type jv to check the version.
Why Does `Java -Version` Print To Stderr Instead Of Stdout?
Java outputs version information to stderr by design. This is because the version info is considered diagnostic output, not program output. When redirecting output, use 2>&1 to capture it.
Can I Check Java Version In Linux Without Java Installed?
No, you need Java installed to run the java command. However, you can check if any Java packages are installed using your package manager (e.g., dpkg -l | grep java on Debian systems).
What Is The Difference Between `Java -Version` And `Javac -Version`?
java -version shows the runtime version (JRE), while javac -version shows the compiler version (JDK). If only JRE is installed, javac may not be available.
How Do I Check Java Version In Linux For A Specific User?
Switch to that user with su - username or sudo -u username bash, then run java -version. The version shown depends on the user’s PATH and environment variables.
Conclusion
Checking Java version in Linux is straightforward once you know the commands. The primary method is java -version, but alternative approaches like checking environment variables or using package managers give you more flexibility. Remember that Java prints version info to stderr, so redirect it when needed.
Whether you’re a developer debugging code or a sysadmin managing servers, these techniques will help you quickly verify Java installations. Keep this guide handy for future reference, and you’ll never struggle with Java version checks again.