杰瑞科技汇

Linux下Java版本如何查看?

Of course! Checking the Java version on a Linux system is a common task. Here’s a comprehensive guide covering the different ways to check your Java version, how to handle multiple versions, and how to set the default Java version.

Linux下Java版本如何查看?-图1
(图片来源网络,侵删)

The Quick and Easy Answer

Open your terminal and run one of these commands. The first one is the most common and recommended.

# The standard, most reliable way
java -version
# Alternative ways
javac -version
echo $JAVA_HOME

Detailed Guide: How to Check Java Version

The Standard Method: java -version

This is the best command to use. It prints the version information for the java executable that is currently in your system's PATH.

How to use it: Open a terminal and type:

java -version

What the output looks like: You will see output similar to this, depending on your installed Java version (OpenJDK, Oracle, etc.).

Linux下Java版本如何查看?-图2
(图片来源网络,侵删)

Example 1: OpenJDK 17

openjdk version "17.0.9" 2025-10-17
OpenJDK Runtime Environment (build 17.0.9+7-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 17.0.9+7-Ubuntu-0ubuntu122.04, mixed mode, sharing)

Example 2: Oracle Java 21

java version "21.0.2" 2025-01-16 LTS
Java(TM) SE Runtime Environment (build 21.0.2+13-LTS-58)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.2+13-LTS-58, mixed mode, sharing)

Example 3: If Java is not installed

The program 'java' can be found in the following packages:
 * default-jre
 * gcj-jre-headless
 * openjdk-11-jre-headless
 * openjdk-8-jre-headless
 * ...
Try: sudo apt install <selected package>

This output tells you that Java isn't installed but suggests packages you can install.

Linux下Java版本如何查看?-图3
(图片来源网络,侵删)

Checking the Compiler Version: javac -version

Sometimes you need to know the version of the Java compiler, especially for development. This should match the runtime version.

How to use it:

javac -version

Example Output:

javac 17.0.9

Checking the JAVA_HOME Environment Variable

The JAVA_HOME environment variable tells applications (like Tomcat, Maven, or Gradle) where your Java Development Kit (JDK) is installed. This is crucial for development environments.

How to use it:

echo $JAVA_HOME

Example Output:

/usr/lib/jvm/java-17-openjdk-amd64

Important Note: If JAVA_HOME is not set, the command will return nothing. This doesn't necessarily mean Java isn't installed, but it's a good practice to set it for development.


Common Scenarios & Troubleshooting

Scenario 1: Multiple Java Versions are Installed

It's very common to have multiple Java versions (e.g., Java 8 for a legacy project and Java 17 for a new one). Linux uses a system of alternatives to manage which version is the "default" one that java -version points to.

List all installed Java versions:

# For Debian/Ubuntu based systems (using update-alternatives)
sudo update-alternatives --config java
# For RedHat/CentOS/Fedora based systems (using alternatives)
sudo alternatives --config java

Example Output:

There are 2 choices for the alternative java (providing /usr/bin/java).
  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-17-openjdk-amd64/bin/java      1711      auto mode
  1            /usr/lib/jvm/java-17-openjdk-amd64/bin/java      1711      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
Press <enter> to keep the current choice[*], or type selection number:

The indicates the currently active version. You can type the number of the version you want to make default and press Enter.

Manually set JAVA_HOME: Even if you change the default java command, you might still need to set JAVA_HOME in your shell profile (.bashrc, .zshrc, etc.) for specific applications.

# Find your desired JDK path (e.g., from the output above)
# Then add this line to your ~/.bashrc file
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
# Apply the changes to your current terminal session
source ~/.bashrc

Scenario 2: java -version works, but javac -version fails

This usually means you have installed the Java Runtime Environment (JRE) but not the Java Development Kit (JDK). The JDK includes the compiler (javac) and other development tools.

Solution: Install the JDK package instead of (or in addition to) the JRE.

  • On Debian/Ubuntu:
    # Install OpenJDK 17 JDK
    sudo apt update
    sudo apt install openjdk-17-jdk
  • On RedHat/CentOS/Fedora:
    # Install OpenJDK 17 JDK
    sudo dnf install java-17-openjdk-devel

Summary Table

Command What It Does Typical Use Case
java -version Shows the version of the default Java runtime. The most common check to see if Java is installed and which version is active.
javac -version Shows the version of the default Java compiler. Checking if the JDK is installed and the compiler version matches the runtime.
echo $JAVA_HOME Shows the path defined in the JAVA_HOME environment variable. Verifying or setting the JDK path for development tools.
sudo update-alternatives --config java Manages which Java version is the system default. Switching between multiple installed Java versions.
分享:
扫描分享到社交APP
上一篇
下一篇