杰瑞科技汇

Linux如何查看Java版本?

Of course! Checking the Java version on a Linux system is a common task. Here’s a comprehensive guide covering the most common methods, what to do if you don't have Java installed, and how to manage multiple versions.

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

The Quick and Easy Answer (Most Common Method)

Open your terminal and type one of the following commands. java -version is the most universal.

# This command shows the version and other information
java -version
# This command is often more concise, showing just the version number
java -version 2>&1 | awk -F '"' '/version/ {print $2}'

Example Output:

openjdk version "17.0.9" 2025-10-15
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)

Detailed Methods Explained

Here are the different ways to check your Java version, explained in detail.

java -version (The Standard Method)

This is the command you should use first. It's the standard way to check the version of the java command that is in your system's PATH.

Linux如何查看Java版本?-图2
(图片来源网络,侵删)
$ java -version
  • What it does: It executes the Java Virtual Machine (JVM) with the -version flag, which causes it to print the version information to the standard error stream (stderr) and then exit.
  • Pros: Simple, universal, and gives you the full version string, including the vendor (e.g., OpenJDK, Oracle) and build details.
  • Cons: Only tells you about the java executable in your PATH. If you have multiple Java versions installed, it doesn't show you all of them.

javac -version (Check the Compiler)

This command checks the version of the Java compiler (javac). This is useful for ensuring your compiler and runtime environment versions match.

$ javac -version
javac 17.0.9

If javac is not found, it means the Java Development Kit (JDK) is not installed, only the Java Runtime Environment (JRE).

$JAVA_HOME (The "Official" Way)

Many applications and build tools (like Maven or Gradle) are configured to use the Java specified by the $JAVA_HOME environment variable. It's good practice to check this variable.

# Check if the JAVA_HOME variable is set
echo $JAVA_HOME
# If it's set, you can check the version of that specific JDK
$JAVA_HOME/bin/java -version

Example Output:

Linux如何查看Java版本?-图3
(图片来源网络,侵删)
/usr/lib/jvm/java-11-openjdk-amd64

If $JAVA_HOME is not set or points to a non-existent directory, it can cause issues for development tools. You can set it in your shell's profile file (e.g., ~/.bashrc, ~/.zshrc).

# Example for setting JAVA_HOME for OpenJDK 17
# Find the path using update-java-alternatives or by looking in /usr/lib/jvm
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
# Add this to your PATH as well
export PATH="$JAVA_HOME/bin:$PATH"

What to Do If Java Isn't Installed

If you run java -version and get an error like command not found, you need to install Java. The easiest way is using your system's package manager.

For Debian / Ubuntu / Mint

Use apt. It's recommended to install the OpenJDK, which is open-source and free.

# Update your package list
sudo apt update
# Install the default JDK (often version 11 or 17)
sudo apt install default-jdk
# Or install a specific version, e.g., OpenJDK 17
sudo apt install openjdk-17-jdk

For RHEL / CentOS / Fedora / Rocky Linux

Use dnf (or yum on older systems).

# Update your package list
sudo dnf update
# Install OpenJDK 17 Development Kit
sudo dnf install java-17-openjdk-devel

For Arch Linux

Use pacman.

# Install OpenJDK 17 (JDK includes JRE)
sudo pacman -S jdk17-openjdk

Managing Multiple Java Versions (The update-java-alternatives method)

On Debian-based systems (Ubuntu, Mint, etc.), the update-alternatives tool is the standard way to manage multiple Java installations and switch between them.

List Installed Java Versions

First, see what versions are registered with the system.

sudo update-alternatives --config java

If you have multiple versions installed, this will show you a menu like this:

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-11-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java      1711      manual mode
Press <enter> to keep the current choice[*], or type selection number:

Switch Between Versions

Simply type the number of the version you want to use and press Enter. The indicates the currently active version.

Manually Register a New Version

If you install a JDK manually (e.g., by downloading a tarball from Oracle or Adoptium), you need to register it with update-alternatives.

# The --install command has the format:
# sudo update-alternatives --install <link> <name> <path> <priority>
# Example: Manually adding a JDK to the alternatives list
sudo update-alternatives --install /usr/bin/java java /path/to/your/new/jdk/bin/java 1700
  • <link>: The system link to create (e.g., /usr/bin/java).
  • <name>: The generic name for the alternative (e.g., java).
  • <path>: The full path to the specific java executable.
  • <priority>: A higher number means higher priority. The highest priority version becomes the default.

Summary Table

Command Purpose Notes
java -version Best for quick check. Shows version of java in your PATH. Most common and universal command.
javac -version Checks the Java compiler version. Useful to confirm JDK is installed.
echo $JAVA_HOME Checks the JAVA_HOME environment variable. Important for development tools.
ls /usr/lib/jvm Lists all installed JVMs (on Debian/Ubuntu). A manual way to see what's on your system.
sudo update-alternatives --config java Best for managing versions. Switches between installed Java versions. Standard on Debian-based systems.
分享:
扫描分享到社交APP
上一篇
下一篇