杰瑞科技汇

Linux下如何查看Java版本?

Of course! The command java -version is used to check the version of Java installed on a Linux system. Here's a detailed breakdown of how to use it, what the output means, and how to handle common issues.

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

The Basic Command

Open your terminal and simply type:

java -version

Then press Enter.


Interpreting the Output

The output will tell you three key things: the version, the vendor, and the Java-specific version details.

Example Output 1: Java 17 (LTS - Long Term Support)

This is a modern, commonly used version.

Linux下如何查看Java版本?-图2
(图片来源网络,侵删)
openjdk version "17.0.10" 2025-01-16
OpenJDK Runtime Environment (build 17.0.10+7-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 17.0.10+7-Ubuntu-0ubuntu122.04, mixed mode, sharing)

What this means:

  • openjdk version "17.0.10": This is the main version number. It's OpenJDK (an open-source implementation of Java) version 17.0.10.
  • OpenJDK Runtime Environment: This is the runtime environment needed to run Java applications.
  • OpenJDK 64-Bit Server VM: This is the Virtual Machine that executes your Java code, optimized for server applications.
  • build 17.0.10+7-Ubuntu-0ubuntu122.04: This is the specific build information, including the distribution (Ubuntu) and its version (22.04).

Example Output 2: Java 11 (LTS)

Another very popular LTS version.

java version "11.0.21" 2025-01-08 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.21+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.21+9-LTS, mixed mode)

What this means:

  • java version "11.0.21": This is Java 11.0.21.
  • Java(TM) SE Runtime Environment: This indicates it's Oracle's Java Standard Edition.
  • Java HotSpot(TM) 64-Bit Server VM: This is Oracle's proprietary high-performance Virtual Machine.

Common Problem: "Command not found"

If you get an error like this:

bash: java: command not found

It means Java is not installed on your system, or the system's PATH environment variable does not include the directory where Java's executables are located.

How to Fix It: Install Java

The easiest way to install Java on most modern Linux distributions (like Ubuntu, Debian, Mint, etc.) is using apt.

Step 1: Update your package list

sudo apt update

Step 2: Install OpenJDK (Recommended) It's generally recommended to use OpenJDK as it's free and open-source. For most projects, the latest LTS (Long Term Support) version is the best choice.

  • To install the latest LTS version (currently Java 17):

    sudo apt install openjdk-17-jdk

    The jdk (Java Development Kit) is recommended because it includes the jre (Java Runtime Environment) plus development tools like the compiler (javac).

  • To install a different version (e.g., Java 11):

    sudo apt install openjdk-11-jdk

Step 3: Verify the Installation After the installation is complete, run the command again. It should now work.

java -version

You might also want to check that the Java compiler is installed:

javac -version

Handling Multiple Java Versions

It's common to have multiple Java versions installed (e.g., Java 11 for a legacy project and Java 17 for a new one). Linux has tools to manage this.

The update-java-alternatives command (for Debian/Ubuntu-based systems)

This command helps you switch between different installed Java versions.

List all installed Java versions:

update-java-alternatives --list

Output will look something like this:

java-1.11.0-openjdk-amd64   1111 /usr/lib/jvm/java-1.11.0-openjdk-amd64
java-1.17.0-openjdk-amd64   1171 /usr/lib/jvm/java-1.17.0-openjdk-amd64

Set the default Java version: To switch to Java 17, use the name from the list above.

sudo update-java-alternatives --set java-1.17.0-openjdk-amd64

Verify the change: Run java -version again to confirm it's now the default.

The alternatives command (A more generic tool)

This is a lower-level tool that update-java-alternatives often uses behind the scenes.

List Java alternatives:

sudo update-alternatives --config java

This will show you a menu to choose from.

Select the desired version by entering its number.


Checking Environment Variables (PATH)

Sometimes, even after installation, java -version might fail. This can happen if the JAVA_HOME variable is set incorrectly or the java executable is not in your system's PATH.

You can check which java executable your shell is using with the which command:

which java

If this returns nothing or a path that doesn't seem right, you may need to adjust your PATH or JAVA_HOME variables in your shell's configuration file (e.g., ~/.bashrc, ~/.zshrc). However, installing Java via a package manager like apt usually handles this automatically for system-wide users.

分享:
扫描分享到社交APP
上一篇
下一篇