杰瑞科技汇

Ubuntu下如何用apt-get安装Java?

Of course! This is a very common task for developers and system administrators on Ubuntu. Here’s a comprehensive guide covering everything from the simple apt-get method to more advanced and recommended approaches.

Ubuntu下如何用apt-get安装Java?-图1
(图片来源网络,侵删)

Summary: Which Method Should You Use?

Method Best For Pros Cons
apt-get Beginners, simple scripts, needing a quick "Java" for a command-line tool. Extremely simple, handles dependencies automatically. You don't control the version; may be outdated.
Snap Most users, especially on Ubuntu Desktop. Easy to manage, gets updates automatically. Easy installation, automatic updates, consistent across distros. Can be heavier, slightly slower startup, confined by default.
SDKMAN! Developers who need to manage multiple Java versions (JDKs, JDKs, JREs). The best for managing multiple versions. Switching is easy. Requires a shell profile setup.
Manual Download When you need a specific version not in repositories, or for enterprise builds. Full control over the version and vendor (Oracle, Eclipse Temurin, etc.). Manual setup of JAVA_HOME and PATH is required.

Method 1: Using apt-get (The Simplest Way)

This is the most straightforward method. It uses Ubuntu's default repositories to install a version of Java provided by the Ubuntu maintainers.

Step 1: Update Package Lists

First, update your local package index to ensure you have the latest information.

sudo apt-get update

Step 2: Install the Default Java Package

The package name is typically default-jdk or default-jre. The jdk (Java Development Kit) includes the jre (Java Runtime Environment) plus tools for compiling and debugging Java code (javac, jdb, etc.). It's almost always what you want.

sudo apt-get install default-jdk

The system will likely install OpenJDK, which is a free, open-source implementation of Java.

Ubuntu下如何用apt-get安装Java?-图2
(图片来源网络,侵删)

Step 3: Verify the Installation

After the installation is complete, you can verify it by checking the Java version.

java -version

You should see output similar to this (the version number may vary):

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)

You can also check that the compiler is available:

javac -version

Step 4: (Optional) Set JAVA_HOME Environment Variable

Some applications and build tools (like Maven or Gradle) require the JAVA_HOME environment variable to be set. apt-get doesn't do this for you automatically.

Ubuntu下如何用apt-get安装Java?-图3
(图片来源网络,侵删)
  1. Find the Java Installation Path: The command update-java-alternatives --list will show you the installed Java versions and their paths.

    update-java-alternatives --list

    Output will look like this:

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

    In this example, java-17-openjdk-amd64 is the current default, and its path is /usr/lib/jvm/java-17-openjdk-amd64.

  2. Set JAVA_HOME in your Profile: Edit your ~/.bashrc or ~/.profile file. Using nano:

    nano ~/.bashrc

    Add the following line to the end of the file, replacing the path with the one you found above:

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

    Save the file (Ctrl+O, Enter) and exit (Ctrl+X).

  3. Apply the Changes: Either open a new terminal or run the following command to load the changes into your current session:

    source ~/.bashrc
  4. Verify JAVA_HOME:

    echo $JAVA_HOME

    It should now print the path you set.


Method 2: Using Snap (Modern & Recommended for Ubuntu)

Snap is a universal software package system developed by Canonical. It's the default way to install many applications on Ubuntu and is excellent for Java as it handles updates and dependencies cleanly.

Step 1: Install OpenJDK via Snap

The snap command makes this incredibly simple. This will install the latest LTS (Long-Term Support) version of OpenJDK.

sudo snap install openjdk

Step 2: Verify the Installation

Check the version to confirm it's installed.

java -version

Step 3: (Optional) Pin the Version

If you don't want snap to automatically update Java to a newer version, you can "pin" it.

sudo snap refresh openjdk --channel=18/stable  # Or whatever version you have installed

To see available channels, run snap info openjdk.


Method 3: Using SDKMAN! (The Developer's Choice)

If you are a developer who works on multiple projects requiring different Java versions, SDKMAN! is the best tool for the job. It allows you to install, switch, and manage multiple SDKs (Software Development Kits), including various Java distributions (OpenJDK, OracleJDK, Amazon Corretto, Eclipse Temurin, etc.).

Step 1: Install SDKMAN!

Run the following command in your terminal. It's a single-line installation script.

curl -s "https://get.sdkman.io" | bash

Step 2: Reload Your Shell

The installation script will ask you to reload your shell's configuration. You can do this by opening a new terminal or by running:

source "$HOME/.sdkman/bin/sdkman-init.sh"

Step 3: Install a Java Version

Now you can install any Java version you need. For example, to install Eclipse Temurin JDK 17:

sdk install java 17.0.10-tem

(You can list all available Java versions with sdk list java)

Step 4: Switch Between Java Versions

This is the killer feature. You can easily switch your default Java version for the current terminal session.

# Set the default Java version for the current shell
sdk default java 17.0.10-tem
# Check the current default
sdk current java
# Output:
#
# Using:
# 17.0.10-tem
# /home/user/.sdkman/candidates/java/current
# Check the version
java -version
# It will now show version 17

Method 4: Manual Installation (For Specific Needs)

This method is for when you need a very specific version or a vendor-specific build (like Oracle JDK) that isn't available in the standard repositories.

  1. Download: Go to the vendor's website (e.g., Oracle, Eclipse Temurin, Amazon Corretto) and download the Linux .tar.gz archive.
  2. Extract: Move the file to a location like /opt and extract it.
    sudo tar -xzf jdk-17.0.10_linux-x64_bin.tar.gz -C /opt
  3. Set Environment Variables: This is the same as the apt-get method, but you must do it manually. Edit ~/.bashrc:
    nano ~/.bashrc

    Add the following lines, replacing the path with your actual Java installation directory:

    export JAVA_HOME=/opt/jdk-17.0.10
    export PATH=$JAVA_HOME/bin:$PATH
  4. Apply and Verify:
    source ~/.bashrc
    java -version

Troubleshooting: "Command not found: java"

If you get this error after installation, it means the Java binary directory is not in your system's PATH.

  1. Find the Java Binaries:

    sudo find / -name "java" -type f 2>/dev/null

    This will list all java executables. Look for one in a bin directory, like /usr/lib/jvm/java-17-openjdk-amd64/bin/java.

  2. Add to PATH: The directory containing the java executable (e.g., /usr/lib/jvm/java-17-openjdk-amd64/bin) needs to be added to your PATH environment variable in your ~/.bashrc or ~/.profile file.

    export PATH=/usr/lib/jvm/java-17-openjdk-amd64/bin:$PATH
分享:
扫描分享到社交APP
上一篇
下一篇