Linux

How to Check Java Version Installed on Linux

Check Java Version Installed on Linux

Java remains an essential component in the Linux ecosystem, powering enterprise applications, web services, and development environments. Whether you’re a system administrator, developer, or Linux enthusiast, knowing which Java version runs on your system is crucial for compatibility, security, and troubleshooting. This comprehensive guide explores multiple methods to check Java versions on Linux systems and understand the information presented.

Why Checking Java Version is Important

Understanding your current Java installation isn’t merely a technical exercise—it directly impacts your system’s functionality and security in several ways.

Security Considerations

Java installations receive regular security updates to patch vulnerabilities. Outdated versions may contain known security flaws that malicious actors actively exploit. By regularly checking your Java version, you can ensure you’re running a release with the latest security patches, protecting your system from potential threats.

Compatibility Requirements

Many applications specify minimum Java version requirements or are designed for specific Java releases. Some enterprise software might require Java 8 for stability, while newer applications may need features only available in Java 11 or 17. Without knowing your current Java version, you risk encountering mysterious compatibility issues and errors that can be difficult to diagnose.

Performance Improvements

Each Java release brings performance optimizations that can significantly impact application responsiveness and resource utilization. Java 9 introduced a more efficient garbage collector, while Java 10 brought application class-data sharing to improve startup times. Staying informed about your Java version helps you leverage these improvements.

Effective Troubleshooting

When encountering Java-related issues, version information serves as critical diagnostic data. Many Java errors stem from version mismatches between components or libraries, and support teams typically request this information first when addressing problems.

Development Environment Consistency

For developers, maintaining consistent Java versions across development, testing, and production environments prevents the frustrating “works on my machine” syndrome. Regular version checking helps ensure this consistency throughout the development pipeline.

Prerequisites

Before exploring the various version-checking methods, ensure you have:

Basic Command Line Knowledge

You’ll need fundamental familiarity with Linux terminal commands—navigating directories, executing commands, and interpreting outputs. Most methods in this guide require only basic terminal operations.

Terminal Access

All techniques require terminal access to your Linux system. Access the terminal through your desktop environment’s application menu or by pressing key combinations like Ctrl+Alt+T on many distributions.

Understanding Java Distributions

Java comes in different flavors on Linux systems:

  • OpenJDK: The open-source reference implementation, most commonly packaged in Linux distributions
  • Oracle JDK: The proprietary implementation with additional features and commercial support
  • IBM Java: Optimized for IBM systems
  • Amazon Corretto: Amazon’s distribution with long-term support

JRE vs. JDK Distinction

It’s important to understand the difference between:

  • Java Runtime Environment (JRE): Contains everything needed to run Java applications
  • Java Development Kit (JDK): Includes the JRE plus development tools for Java programming

The version-checking methods may reveal which component you have installed, as they report slightly different information.

Method 1: Using the java -version Command

The most direct and universal approach to check Java on any Linux distribution is the java -version command.

Step-by-step Instructions

  1. Open your terminal application
  2. Type the following command and press Enter:
java -version
  1. Review the displayed output

This simple command queries the default Java runtime and displays detailed version information.

Understanding the Output

A typical output might appear as:

openjdk version "11.0.14" 2022-01-18
OpenJDK Runtime Environment (build 11.0.14+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.14+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

This output provides several key pieces of information:

  • First line: Shows the Java distribution (OpenJDK) and major version (11.0.14)
  • Second line: Contains runtime environment details and build information
  • Third line: Provides JVM implementation details, architecture (64-bit), and operating mode

Examples from Different Java Distributions

Oracle JDK might display:

java version "1.8.0_301"
Java(TM) SE Runtime Environment (build 1.8.0_301-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.301-b09, mixed mode)

Amazon Corretto might show:

openjdk version "11.0.14.1" 2022-02-08 LTS
OpenJDK Runtime Environment Corretto-11.0.14.10.1 (build 11.0.14.1+10-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.14.10.1 (build 11.0.14.1+10-LTS, mixed mode)

Troubleshooting Command Not Found

If you receive a “command not found” error, it indicates that:

  1. Java is not installed on your system
  2. Java is installed but not in your system’s PATH
  3. Your distribution requires different package names

To resolve this issue:

  • Install Java using your distribution’s package manager (e.g., sudo apt install default-jre for Debian/Ubuntu)
  • Check your PATH variable with echo $PATH to verify Java’s location is included
  • Use which java to locate the Java binary if it exists

Using java -fullversion

For more detailed version information, especially useful in scripts, use:

java -fullversion

This produces a single line output containing the full version string, making it easier to parse programmatically.

Method 2: Checking Java Compiler Version

While runtime verification is common, developers often need to verify their compiler version for development tasks.

Using javac -version

To check the Java compiler version, execute:

javac -version

This command returns the version of the Java compiler (javac) installed on your system.

Why Check Compiler Version

The compiler version matters particularly for developers because:

  1. It determines which language features are available for your code
  2. It affects compatibility with libraries and frameworks
  3. It ensures your build environment matches your runtime environment

Sample Output

A typical output appears simply as:

javac 11.0.14

Unlike the runtime output, this format is concise, showing only the compiler version without additional build information.

When Compiler and Runtime Versions Differ

In some scenarios, particularly in development environments, you might have mismatched compiler and runtime versions. This situation can arise when:

  • You’ve installed multiple JDK versions and configured them differently
  • Your system uses one Java version by default but your IDE references another
  • You’ve updated your JRE but not your JDK, or vice versa

Such mismatches can lead to compatibility issues where code compiles successfully but fails at runtime.

Method 3: Using update-alternatives

Linux distributions using the alternatives system (like Debian, Ubuntu, Fedora, and CentOS) offer powerful management capabilities for multiple Java installations.

Command Syntax for Listing Java Alternatives

To list all installed Java installations registered in the alternatives system:

update-alternatives --list java

This shows all Java versions managed by your system’s alternatives mechanism.

Purpose and Benefits

The alternatives system allows you to:

  1. Maintain multiple Java versions concurrently
  2. Switch between different versions system-wide
  3. Configure default versions for specific Java components
  4. Track manually installed Java versions

Output Interpretation

A typical output might appear as:

/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
/usr/lib/jvm/java-17-openjdk-amd64/bin/java

Each line represents a distinct Java installation with its full path, revealing:

  • Available Java versions (8, 11, and 17 in this example)
  • Distribution type (OpenJDK)
  • System architecture (amd64)
  • Exact location of each Java binary

Checking All Java-related Alternatives

For a complete picture of your Java environment, check alternatives for other components:

update-alternatives --list javac    # Java compiler
update-alternatives --list jar      # Java archive tool
update-alternatives --list javadoc  # Java documentation generator

Switching Between Java Versions

To change your default Java version:

sudo update-alternatives --config java

This interactive command presents available Java versions, allowing you to select which one should be the system default.

Method 4: Finding Java in Installed Packages

Package managers maintain detailed records of installed software, providing another source of information about your Java installations.

Using Package Managers

Different Linux distributions use different package managers:

  • Debian/Ubuntu: apt
  • Fedora: dnf
  • CentOS/RHEL: yum or dnf
  • Arch Linux: pacman

Commands for Different Package Managers

For Debian/Ubuntu systems:

apt list --installed | grep -i 'java\|openjdk\|oracle'

For Fedora or modern CentOS:

dnf list installed | grep -i 'java\|openjdk\|oracle'

For older CentOS/RHEL systems:

yum list installed | grep -i 'java\|openjdk\|oracle'

Filtering and Understanding Results

Focus on packages with names containing:

  • openjdk: Open Java Development Kit
  • java: General Java-related packages
  • jre: Java Runtime Environment components
  • jdk: Java Development Kit components

Package Naming Conventions

Package names typically include version information, though formats vary by distribution:

  • Debian/Ubuntu: openjdk-11-jre indicates OpenJDK version 11, JRE component
  • Fedora/CentOS: java-11-openjdk indicates OpenJDK version 11
  • Version numbers might appear as 1.8.0 (Java 8) or directly as 11 (Java 11)

Distribution-specific Differences

Be aware that distributions package Java differently:

  • Ubuntu typically separates JRE and JDK into distinct packages
  • Some distributions bundle debugging tools separately
  • Enterprise distributions might include additional security patches
  • Default versions vary significantly between distributions

Method 5: Locating Java Installation Path

Understanding exactly where Java resides on your filesystem helps with troubleshooting and configuration.

Using which java

To find the location of your Java executable:

which java

This returns the path to the Java binary that executes when you type java at the command line.

Following Symbolic Links

On most Linux systems, the path returned by which java is actually a symbolic link. To find the actual binary:

ls -l $(which java)

You might see output like:

lrwxrwxrwx 1 root root 22 Feb 10 09:14 /usr/bin/java -> /etc/alternatives/java

To follow the complete chain:

ls -l /etc/alternatives/java

Which might show:

lrwxrwxrwx 1 root root 43 Feb 10 09:14 /etc/alternatives/java -> /usr/lib/jvm/java-11-openjdk-amd64/bin/java

Complete Path Resolution

To resolve the complete path in one command:

readlink -f $(which java)

This follows all symbolic links and returns the absolute path to the actual Java binary.

Checking JAVA_HOME Environment Variable

Many applications rely on the JAVA_HOME environment variable:

echo $JAVA_HOME

If this returns a value, it indicates the Java installation directory used by applications that depend on this variable.

Manual Path Exploration

Once you’ve found the Java binary location, explore the surrounding directory structure:

# Find the Java installation root
cd "$(dirname "$(readlink -f "$(which java)")")/.."
pwd
ls -la

This typically reveals the root of your Java installation with directories like:

  • bin/: Executable tools
  • lib/: Libraries and resources
  • include/: Header files for native code
  • jre/: (In older JDK installations) The runtime environment

Understanding Java Version Output

The output from java -version contains valuable information beyond just the version number.

Version Number Format

Java version numbers follow specific patterns:

  • Legacy format (Java 8 and earlier): 1.8.0_301
    • 1 is the major version (always 1 for older versions)
    • 8 is the feature release number
    • 0 is the maintenance release number
    • _301 is the update number
  • Modern format (Java 9 and later): 11.0.14
    • 11 is the feature release number
    • 0 is the interim release number
    • 14 is the update release number

Build Information

The build string provides details about when and how the Java release was created:

(build 11.0.14+9-Ubuntu-0ubuntu2.20.04)

In this example:

  • 11.0.14 is the version number
  • +9 is the build number
  • Ubuntu-0ubuntu2.20.04 indicates distribution-specific modifications

JVM Details

The JVM line contains information about the Java Virtual Machine implementation:

OpenJDK 64-Bit Server VM (build 11.0.14+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

Key components include:

  • JVM implementation (OpenJDK 64-Bit Server VM)
  • Architecture (64-Bit)
  • Operating mode (Server VM)
  • Execution mode (mixed mode)
  • Class data sharing setting (sharing)

Distribution Identifiers

Different Java distributions identify themselves uniquely:

  • OpenJDK: Shows “openjdk version”
  • Oracle: Shows “java version” with Java(TM) branding
  • IBM: Includes “IBM” in the version string
  • Amazon Corretto: Includes “Corretto” in the build information

Release Date Information

Some Java versions include the release date in the version output:

openjdk version "11.0.14.1" 2022-02-08 LTS

Here, “2022-02-08” indicates the release date, and “LTS” designates a Long-Term Support release.

Managing Multiple Java Versions

As your Java needs evolve, you’ll likely need to manage multiple Java versions on a single Linux system.

Common Scenarios for Multiple Versions

Several situations require multiple Java installations:

  • Supporting legacy applications needing older Java versions
  • Developing applications targeting different Java platforms
  • Testing compatibility across Java versions
  • Using version-specific features while maintaining backward compatibility

Using the Alternatives System

The alternatives system provides a consistent way to manage multiple Java versions:

# Set the default Java version
sudo update-alternatives --config java

# Set the default Java compiler
sudo update-alternatives --config javac

For manual installation, register a new Java version:

sudo update-alternatives --install /usr/bin/java java /path/to/new/java/bin/java 100

The number (100) represents the priority—higher numbers take precedence in automatic selection.

Environment Variables for Temporary Switching

For temporary or user-specific Java selection:

# Set a specific Java version for the current session
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

# Verify the change
java -version

This affects only the current terminal session without changing system-wide settings.

Application-Specific Configurations

Many applications allow specifying which Java version to use:

  • Tomcat: Set JAVA_HOME in setenv.sh
  • Maven: Configure JAVA_HOME in ~/.mavenrc
  • Gradle: Use the org.gradle.java.home property
  • IntelliJ IDEA: Set the project SDK in Project Structure settings

Best Practices for Version Management

To maintain a clean and manageable Java environment:

  1. Use your distribution’s package manager when possible
  2. Document installed versions and their purposes
  3. Regularly audit and remove unused Java installations
  4. Consider version managers like jenv or jabba for complex setups

Checking Java in Web Browsers

While browser-based Java has been largely deprecated, some legacy applications still use it.

Browser Plugins vs. System Java

Browser Java implementations are separate from your system’s Java installation:

  • They run in sandboxed environments
  • Have their own update mechanisms
  • May have different security settings
  • Are version-controlled independently

Browser-Specific Version Checking

Modern browsers have either removed or restricted Java plugin support:

  • Firefox: Type about:plugins in the address bar (older versions)
  • Chrome: Type chrome://plugins (older versions before plugin removal)
  • Edge: Does not support Java plugins

For browsers that still support Java, visit https://www.java.com/verify/ to check the plugin version.

Security Considerations

Browser-based Java poses significant security risks:

  • Java applets can request elevated permissions
  • Outdated browser plugins are common attack vectors
  • Modern browsers block Java content by default
  • Oracle has deprecated the Java browser plugin

Modern Alternatives

Instead of Java applets, modern web applications use:

  • WebAssembly for near-native performance
  • JavaScript frameworks for rich client-side functionality
  • HTML5 technologies that replace former Java plugin features

Troubleshooting Common Issues

Even experienced Linux users occasionally encounter Java-related problems.

No Java Found

If java -version returns “command not found”:

  1. Install Java using your distribution’s package manager:
# Ubuntu/Debian
sudo apt update
sudo apt install default-jre

# Fedora
sudo dnf install java-latest-openjdk

# CentOS
sudo yum install java-11-openjdk
  1. Verify successful installation:
java -version

Path and Environment Variable Problems

If Java is installed but not found or the wrong version runs:

  1. Check your PATH variable:
echo $PATH
  1. Verify JAVA_HOME is correctly set:
echo $JAVA_HOME
  1. Add Java to your PATH temporarily:
export PATH=/usr/lib/jvm/your-java-version/bin:$PATH
  1. Add to .bashrc for persistence:
echo 'export JAVA_HOME=/usr/lib/jvm/your-java-version' >> ~/.bashrc
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

Permission Issues

Java executables might have incorrect permissions:

  1. Check file permissions:
ls -l $(which java)
  1. Fix if necessary:
sudo chmod +x $(which java)

Conflicting Versions

When multiple Java versions conflict:

  1. List all installed versions:
update-alternatives --list java
  1. Select the appropriate default:
sudo update-alternatives --config java
  1. Remove unwanted versions if necessary:
sudo apt remove openjdk-8-jre  # Example for Ubuntu

Broken Installations

For corrupted Java installations:

  1. Purge and reinstall:
# Ubuntu/Debian
sudo apt purge openjdk*
sudo apt autoremove
sudo apt install default-jre
  1. Check for orphaned configurations:
sudo update-alternatives --remove-all java

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button