杰瑞科技汇

Linux下如何查看Java版本?

Of course! The command java --version is used to check the version of the Java Runtime Environment (JRE) installed on your Linux system.

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

Here is a comprehensive guide covering different scenarios, from the simple command to troubleshooting common issues.


The Basic Command

Open your terminal and type the following command, then press Enter:

java --version

Expected Output:

If Java is correctly installed and configured in your system's PATH, you will see output similar to one of these:

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

Example 1 (Java 17 - LTS):

openjdk 17.0.10 2025-01-16
OpenJDK Runtime Environment (build 17.0.10+7-Ubuntu-120.04)
OpenJDK 64-Bit Server VM (build 17.0.10+7-Ubuntu-120.04, mixed mode, sharing)

Example 2 (Java 11 - LTS):

openjdk 11.0.23 2025-04-16
OpenJDK Runtime Environment (build 11.0.23+9-Ubuntu-0ubuntu1~20.04)
OpenJDK 64-Bit Server VM (build 11.0.23+9-Ubuntu-0ubuntu1~20.04, mixed mode, sharing)

Example 3 (Oracle Java 17):

java 17.0.10 2025-01-16
Java(TM) SE Runtime Environment (build 17.0.10+11-LTS-240)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.10+11-LTS-240, mixed mode, sharing)

Troubleshooting: What if it Doesn't Work?

If you get an error like "Command 'java' not found" or "bash: java: command not found", it means Java is either not installed or the system doesn't know where to find it.

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

Step 1: Check if Java is Installed at All

Try the which command to see if the java executable exists in any of the directories listed in your PATH:

which java
  • If it returns a path (e.g., /usr/bin/java): The executable exists, but the system might not be able to find it due to a misconfiguration. Proceed to Step 3.
  • If it returns nothing: Java is likely not installed. Proceed to Step 2.

Step 2: Install Java

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

Update your package list:

sudo apt update

Install a Java Development Kit (JDK). It's highly recommended to install a JDK (which includes the JRE) instead of just the JRE, as many development tools require it.

# Install the default OpenJDK (e.g., OpenJDK 17)
sudo apt install default-jdk
# Or install a specific version, e.g., OpenJDK 11
# sudo apt install openjdk-11-jdk

For other distributions:

  • Fedora/CentOS/RHEL: Use dnf or yum.
    sudo dnf install java-17-openjdk-devel
  • Arch Linux: Use pacman.
    sudo pacman -S jdk-openjdk

After installation, try running java --version again.

Step 3: Set the JAVA_HOME Environment Variable

Even if java --version works, some applications (like Apache Maven, Tomcat, or Spring Boot) require the JAVA_HOME environment variable to be set to the root directory of your JDK installation.

Find your Java Installation Path

Run this command to find the path:

readlink -f $(which java)

This will output something like /usr/lib/jvm/java-17-openjdk-amd64/bin/java. The JAVA_HOME should be the directory above the bin directory.

So, in this example, JAVA_HOME would be: /usr/lib/jvm/java-17-openjdk-amd64

Set JAVA_HOME Permanently

The best way to do this is to edit the profile file for all users or for your specific user.

For all users (requires sudo): Edit the /etc/environment file:

sudo nano /etc/environment

Add the following line at the end of the file. Replace the path with your own.

JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"

Save the file (Ctrl+O, Enter) and exit (Ctrl+X). You will need to log out and log back in for this to take effect, or run source /etc/environment.

For your current user only: Edit your shell's configuration file (e.g., ~/.bashrc or ~/.zshrc):

nano ~/.bashrc

Add the following line at the end of the file:

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH="$JAVA_HOME/bin:$PATH"

Save the file and then apply the changes to your current terminal session by running:

source ~/.bashrc

Managing Multiple Java Versions

It's common to have multiple Java versions installed (e.g., Java 8 for an old project and Java 17 for a new one). The update-alternatives tool helps you manage this.

See what versions are registered:

sudo update-alternatives --config java

This will show you a list of installed Java versions and let you choose which one is the default.

Manually add a version (if not detected automatically): If you installed Java manually, you might need to register it.

# Replace with the actual paths to your Java installations
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-17-openjdk-amd64/bin/java 2

Then, run sudo update-alternatives --config java to switch between them.

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