Installing Java on Ubuntu – 在Ubuntu上安装Java

最后修改: 2018年 11月 7日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll introduce different methods for installing a JDK on Ubuntu. Then, we’ll briefly compare the methods. Finally, we’ll show how to manage multiple Java installations on an Ubuntu system.

在本教程中,我们将介绍在Ubuntu上安装JDK的不同方法。然后,我们将简要地比较这些方法。最后,我们将展示如何在Ubuntu系统上管理多个Java安装。

As a prerequisite to each method, we need

作为每种方法的前提条件,我们需要

  • an Ubuntu system
  • to be logged in as a non-root user with sudo privileges

The instructions described below have been tested on Ubuntu 18.10, 18.04 LTS, 16.04 LTS, and 14.04 LTS. For Ubuntu 14.04 LTS, there’re some differences, which are mentioned in the text.

下面描述的说明已经在Ubuntu 18.10、18.04 LTS、16.04 LTS和14.04 LTS上测试过。对于Ubuntu 14.04 LTS,有一些区别,在文中会提到。

Please note that both the packages you can download from OpenJDK and Oracle and the packages available in repositories are updated regularly. The exact package names will probably change within some months, but the basic methods of installation will remain the same.

请注意,你可以从OpenJDK和Oracle下载的包以及存储库中的包都会定期更新。确切的软件包名称可能会在几个月内改变,但基本的安装方法将保持不变。

2. Installing JDK 11

2.安装JDK 11

If we want to use the latest and greatest version of JDK, often manual installation is the way to go. This means downloading a package from the OpenJDK or the Oracle site and setting it up so that it adheres to the conventions of how apt sets up the JDK packages.

如果我们想使用最新、最好的 JDK 版本,通常手动安装才是正确的做法。这意味着从 OpenJDK 或 Oracle 网站下载一个包,并对其进行设置,使其符合 apt 设置 JDK 包的惯例。

2.1. Installing OpenJDK 11 Manually

2.1.手动安装OpenJDK 11

First of all, let’s download the tar archive of the recently released OpenJDK 11:

首先,让我们下载最近发布的OpenJDK 11的tar存档。

$ wget https://download.java.net/java/ga/jdk11/openjdk-11_linux-x64_bin.tar.gz

And we compare the sha256 sum of the downloaded package with the one provided on the OpenJDK site:

而我们将下载的软件包的sha256OpenJDK网站上提供的进行比较。

$ sha256sum openjdk-11_linux-x64_bin.tar.gz

Let’s extract the tar archive:

让我们提取tar存档。

$ tar xzvf openjdk-11_linux-x64_bin.tar.gz

Next, let’s move the jdk-11 directory we’ve just extracted into a subdirectory of /usr/lib/jvm. The apt packages described in the next section also put their JDKs into this directory:

接下来,让我们把刚刚提取的jdk-11目录移到/usr/lib/jvm的一个子目录下。下一节中描述的apt包也将它们的JDK放到这个目录中。

$ sudo mkdir /usr/lib/jvm
$ sudo mv jdk-11 /usr/lib/jvm/openjdk-11-manual-installation/

Now, we want to make the java and javac commands available. One possibility would be to create symbolic links for them, for example, in the /usr/bin directory. But instead, we’ll install an alternative for both of them. This way, if we ever wish to install additional versions of JDK, they will play nicely together:

现在,我们想使javajavac命令可用。一种可能性是为它们创建符号链接,例如,在/usr/bin目录下。但相反,我们将为它们安装一个替代品。这样,如果我们想安装其他版本的JDK,它们就可以很好地一起运行。

$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/openjdk-11-manual-installation/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/openjdk-11-manual-installation/bin/javac 1

Let’s verify the installation:

让我们验证一下安装。

$ java -version

As we can see from the output, we’ve indeed installed the latest version of the OpenJDK JRE and JVM:

从输出结果可以看出,我们确实已经安装了最新版本的OpenJDK JRE和JVM。

openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)

Let’s have a look at the compiler version also:

让我们也来看看编译器的版本。

$ javac -version
javac 11

2.2. Installing Oracle JDK 11 Manually

2.2.手动安装Oracle JDK 11

If we want to make sure to use the newest version of Oracle JDK, we can follow a similar manual installation workflow, as for OpenJDK. In order to download the tar archive for JDK 11 from the Oracle website, we must accept a license agreement first. For this reason, downloading via wget is a bit more complicated than for OpenJDK:

如果我们想确保使用最新版本的Oracle JDK,我们可以遵循类似于OpenJDK的手动安装工作流程。为了从Oracle网站下载JDK 11的tar存档,我们必须先接受一份许可协议。由于这个原因,通过wget下载要比OpenJDK复杂一些。

$ wget -c --header "Cookie: oraclelicense=accept-securebackup-cookie" \
http://download.oracle.com/otn-pub/java/jdk/11.0.1+13/90cf5d8f270a4347a95050320eef3fb7/jdk-11.0.1_linux-x64_bin.tar.gz

The example above downloads the package for 11.0.1 The exact download link changes for each minor version.

上面的例子下载了11.0.1的软件包,每个小版本的确切下载链接都会改变。

The following steps are the same as for OpenJDK:

以下步骤与OpenJDK的步骤相同。

$ sha256sum jdk-11.0.1_linux-x64_bin.tar.gz
$ tar xzvf jdk-11.0.1_linux-x64_bin.tar.gz
$ sudo mkdir /usr/lib/jvm
$ sudo mv jdk-11.0.1 /usr/lib/jvm/oracle-jdk-11-manual-installation/
$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/oracle-jdk-11-manual-installation/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/oracle-jdk-11-manual-installation/bin/javac 1

The verification is also the same. But the output shows that this time, we’ve installed not OpenJDK but Java(TM):

验证结果也是一样的。但输出结果显示,这次我们安装的不是OpenJDK而是Java(TM)。

$ java -version
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

And for the compiler:

而对于编译器。

$ javac -version
javac 11.0.1

2.3. Installing Oracle JDK 11 from a PPA

2.3.从PPA中安装Oracle JDK 11

Currently, Oracle JDK 11 is also available in a PPA (personal package archive). This installation involves 2 steps: adding the repository to our system and installing the package from the repository via apt:

目前,Oracle JDK 11也可以通过PPA(个人软件包存档)获得。这种安装包括2个步骤:将存储库添加到我们的系统中,并通过apt:从存储库安装软件包。

$ sudo add-apt-repository ppa:linuxuprising/java
$ sudo apt update
$ sudo apt install oracle-java11-installer

The verifying steps should show the same result as after the manual installation in section 2.2.1.:

验证步骤应显示与2.2.1节中手动安装后的结果相同。

$ java -version
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

And for the compiler:

而对于编译器。

$ javac -version
javac 11.0.1

On Ubuntu 14.04 LTS the add-apt-repository command isn’t available by default. In order to add a repository, first we need to install the software-properties-common package.

在Ubuntu 14.04 LTS上,add-apt-repository命令默认是不可用的。为了添加一个软件库,首先我们需要安装software-properties-common包。

$ sudo apt update
$ sudo apt install software-properties-common

Afterward, we can continue with add-apt-repository, apt update and apt install as shown above.

之后,我们可以继续进行add-apt-repository, apt updateapt install,如上图所示。

3. Installing JDK 8

3.安装JDK 8

3.1. Installing OpenJDK 8 on Ubuntu 16.04 LTS and Newer

3.1.在Ubuntu 16.04 LTS和更新版本上安装OpenJDK 8

JDK 8 is an LTS version that has been around for a while. For this reason, we can find an up-to-date version of OpenJDK 8 in the “Main” repository on most of the supported Ubuntu versions. Of course, we can also head to the OpenJDK website, grab a package there, and install it the same way we’ve seen in the previous section.

JDK 8是一个LTS版本,已经存在了一段时间。出于这个原因,我们可以在大多数支持的Ubuntu版本的 “主 “仓库中找到最新版本的OpenJDK 8。当然,我们也可以前往OpenJDK网站,在那里抓取一个软件包,然后按照我们在上一节中看到的方式进行安装。

But using the apt tooling and the “Main” repository provides some benefits. The “Main” repository is available by default on all Ubuntu systems. It’s supported by Canonical — the same company that maintains Ubuntu itself.

但使用apt工具和 “主 “版本库提供了一些好处。所有Ubuntu系统的 “主 “版本库都是默认可用的。它得到了Canonical的支持–也就是维护Ubuntu的那家公司。

Let’s install OpenJDK 8 from the “Main” repository with apt:

让我们用apt从 “Main “资源库中安装OpenJDK 8。

$ sudo apt update
$ sudo apt install openjdk-8-jdk

Now, let’s verify the installation:

现在,让我们验证一下安装情况。

$ java -version

The result should list a Runtime Environment and a JVM:

结果应该列出一个运行时环境和一个JVM。

openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-0ubuntu0.18.04.1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

Let’s check that the javac executable is available as well:

让我们检查一下javac的可执行文件是否也可用。

$ javac -version

Now we should see the same version number as shown above:

现在我们应该看到与上面所示相同的版本号。

javac 1.8.0_181

3.2. Installing OpenJDK 8 on Ubuntu 14.04 LTS

3.2. 在Ubuntu 14.04 LTS上安装OpenJDK 8

On Ubuntu 14.04 LTS, the OpenJDK packages aren’t available in the “Main” repository, so we’ll install them from the openjdk-r PPA. As we’ve seen in section 2.3 above, the add-apt-repository command isn’t available by default. We need the software-properties-common package for it:

在Ubuntu 14.04 LTS上,OpenJDK包在 “主 “仓库中不可用,所以我们将从openjdk-r PPA中安装它们。正如我们在上面第2.3节中所看到的,add-apt-repository命令默认是不可用的。我们需要software-properties-common包来实现它。

$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:openjdk-r/ppa
$ sudo apt update
$ sudo apt install openjdk-8-jdk

3.3. Installing Oracle JDK 8 from a PPA

3.3.从PPA中安装Oracle JDK 8

The “Main” repository does not contain any proprietary software. If we want to install Oracle Java with apt, we’ll have to use a package from a PPA. We’ve already seen how to install Oracle JDK 11 from the linuxuprising PPA. For Java 8, we can find the packages in the webupd8team PPA.

主 “资源库不包含任何专有软件。如果我们想用apt安装Oracle Java,我们必须使用PPA的软件包。我们已经看到如何从linuxuprising PPA中安装Oracle JDK 11。对于Java 8,我们可以在webupd8team PPA中找到这些包。

First, we need to add the PPA apt repository to our system:

首先,我们需要将PPA apt 仓库添加到我们的系统中。

$ sudo add-apt-repository ppa:webupd8team/java

Then we can install the package the usual way:

然后我们就可以用通常的方法来安装这个软件包。

$ sudo apt update
$ sudo apt install oracle-java8-installer

During the installation, we have to accept Oracle’s license agreement. Let’s verify the installation:

在安装过程中,我们必须接受Oracle的许可协议。让我们验证一下安装。

$ java -version

The output shows a Java(TM) JRE and JVM:

输出显示了一个Java(TM) JRE和JVM。

java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

We can also verify that the compiler has been installed:

我们还可以验证编译器是否已经安装。

$ javac -version
javac 1.8.0_181

4. Installing JDK 10

4.安装JDK 10

The versions Java 10 and Java 9 aren’t supported anymore. You can install them manually, following similar steps as in section 2. You can grab the packages from:

Java 10和Java 9的版本已经不被支持。你可以按照第2节中的类似步骤手动安装它们。你可以从以下地方抓取这些软件包。

Both sites contain the same warning:

两个网站都包含相同的警告。

These older versions of the JDK are provided to help developers debug issues in older systems. They are not updated with the latest security patches and are not recommended for use in production.

提供这些旧版本的 JDK 是为了帮助开发人员调试旧系统中的问题。它们没有更新最新的安全补丁,不建议在生产中使用。

4.1.  Installing OpenJDK 10 Manually

4.1. 手动安装OpenJDK 10

Let’s see how to install OpenJDK 10.0.1:

让我们看看如何安装OpenJDK 10.0.1。

$ wget https://download.java.net/java/GA/jdk10/10.0.1/fb4372174a714e6b8c52526dc134031e/10/openjdk-10.0.1_linux-x64_bin.tar.gz
$ sha256sum openjdk-10.0.1_linux-x64_bin.tar.gz
$ tar xzvf openjdk-10.0.1_linux-x64_bin.tar.gz
$ sudo mkdir /usr/lib/jvm
$ sudo mv jdk-10.0.1 /usr/lib/jvm/openjdk-10-manual-installation/
$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/openjdk-10-manual-installation/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/openjdk-10-manual-installation/bin/javac 1
$ java -version
$ javac -version

4.2. Installing Oracle JDK 10 Manually

4.2.手动安装Oracle JDK 10

As we’ve seen in section 2.2., in order to download a package from the Oracle website, we must accept a license agreement first. Contrary to the supported versions, we can’t download the older Oracle JDKs via wget and a cookie. We need to head to https://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase10-4425482.html and download the tar.gz file. Afterward, we follow the familiar steps:

正如我们在第2.2节中所看到的,为了从Oracle网站上下载一个包,我们必须先接受一个许可协议。与支持的版本相反,我们不能通过wget和cookie下载旧的Oracle JDK。我们需要前往https://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase10-4425482.html并下载tar.gz文件。之后,我们遵循熟悉的步骤。

$ sha256sum jdk-10.0.2_linux-x64_bin.tar.gz
$ tar xzvf jdk-10.0.2_linux-x64_bin.tar.gz
$ sudo mkdir /usr/lib/jvm
$ sudo mv jdk-10.0.2 /usr/lib/jvm/oracle-jdk-10-manual-installation/
$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/oracle-jdk-10-manual-installation/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/oracle-jdk-10-manual-installation/bin/javac 1
$ java -version
$ javac -version

5. Installing JDK 9

5.安装JDK 9

5.1. Installing OpenJDK 9 Manually

5.1.手动安装OpenJDK 9

Just like we saw above with OpenJDK 10.0.1, we download the OpenJDK 9 package via wget and set it up according to the conventions:

就像我们在上面看到的OpenJDK 10.0.1一样,我们通过wget下载OpenJDK 9包,并按照惯例进行设置。

$ wget https://download.java.net/java/GA/jdk9/9.0.4/binaries/openjdk-9.0.4_linux-x64_bin.tar.gz
$ sha256sum openjdk-9.0.4_linux-x64_bin.tar.gz
$ tar xzvf openjdk-9.0.4_linux-x64_bin.tar.gz
$ sudo mkdir /usr/lib/jvm
$ sudo mv jdk-9.0.4 /usr/lib/jvm/openjdk-9-manual-installation/
$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/openjdk-9-manual-installation/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/openjdk-9-manual-installation/bin/javac 1
$ java -version
$ javac -version

5.2. Installing Oracle JDK 9 Manually

5.2.手动安装Oracle JDK 9

Once again, we use the same method as for JDK 10. We need to head to https://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase9-3934878.html and download the tar.gz file. Afterward, we follow the familiar steps:

再一次,我们使用与JDK 10相同的方法。我们需要前往https://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase9-3934878.html并下载tar.gz文件。之后,我们遵循熟悉的步骤。

$ sha256sum jdk-9.0.4_linux-x64_bin.tar.gz
$ tar xzvf jdk-9.0.4_linux-x64_bin.tar.gz
$ sudo mkdir /usr/lib/jvm
$ sudo mv jdk-9.0.4 /usr/lib/jvm/oracle-jdk-9-manual-installation/
$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/oracle-jdk-9-manual-installation/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/oracle-jdk-9-manual-installation/bin/javac 1
$ java -version
$ javac -version

6. Comparison

6.比较

We’ve seen three different ways of installing a JDK on Ubuntu. Let’s have a quick overview of each of them, pointing out the advantages and disadvantages.

我们已经看到在Ubuntu上安装JDK的三种不同方式。让我们快速浏览一下它们,指出它们的优点和缺点。

6.1. “Main” Repository

6.1.”主 “存储库

This is the “Ubuntu native” way of installation. A big advantage is that we update the packages via the “usual apt workflow” with apt update and apt upgrade.

这是“Ubuntu本地 “的安装方式。一个很大的优点是,我们通过apt工作流程apt updateapt upgrade来更新软件包。

Furthermore, the “Main” repository is maintained by Canonical, which provides reasonably fast (if not immediate) updates. For example, OpenJDK versions 10.0.1 and 10.0.2 were both synced within a month of release.

此外,”主 “版本库由Canonical维护,提供了相当快的(如果不是立即)更新。例如,OpenJDK 10.0.1和10.0.2版本都是在发布后一个月内同步的。

6.2. PPA

6.2. PPA

PPAs are small repositories maintained by an individual developer or a group. This also means that the update frequency depends on the maintainer.

PPA是小型存储库,由个别开发者或团体维护。这也意味着,更新频率取决于维护者。

Packages from PPAs are considered riskier than the packages in the “Main” repository. First, we have to add the PPA explicitly to the system’s repository list, indicating that we trust it. Afterward, we can manage the packages via the usual apt tooling (apt update and apt upgrade).

来自PPA的软件包被认为比 “主 “版本库中的软件包更危险。首先,我们必须将PPA明确地添加到系统的仓库列表中,表明我们信任它。之后,我们可以通过通常的apt工具(apt updateapt upgrade)管理这些软件包。

6.3. Manual Installation

6.3.手动安装

We download the package directly from the OpenJDK or Oracle site. Although this method offers a great deal of flexibility, updates are our responsibility. If we want to have the latest and greatest JDK, this is the way to go.

我们直接从 OpenJDK 或 Oracle 网站下载软件包。尽管这种方法提供了很大的灵活性,但更新是我们的责任。如果我们想拥有最新、最棒的 JDK,这就是我们的方法。

7. Exploring Other Versions of JDKs

7.探索其他版本的JDKs

The examples in sections 2 and 3 reflect the current status on Ubuntu 18.04 LTS. Keep in mind that the JDKs and the corresponding packages are updated regularly. Thus it’s useful to know how to explore our current possibilities.

第2节和第3节中的例子反映了Ubuntu 18.04 LTS上的当前状态。请记住,JDKs和相应的软件包会定期更新。因此,了解如何探索我们当前的可能性是很有用的。

In this section, we’ll focus on surveying the OpenJDK packages in the “Main” repository. If we’ve already added a PPA with add-apt-repository, we can explore it in a similar manner with apt list and apt show.

在本节中,我们将重点调查 “主 “仓库中的 OpenJDK 包。如果我们已经用add-apt-repository添加了一个PPA,我们可以用apt listapt show的类似方式来探索它。

To discover which PPAs are available, we can head to https://launchpad.net/. If we don’t find what we’re looking for in the “Main” repository and in the PPAs, we’ll have to fall back to manual installation.

要发现哪些PPA是可用的,我们可以前往https://launchpad.net/。如果我们在 “主 “软件库和PPA中没有找到我们要找的东西,我们将不得不退回到手动安装。

If we’d like to use an unsupported version, even that can be difficult. As of this writing, we didn’t find any packages for Java 9 or Java 10 on the OpenJDK and Oracle websites.

如果我们想使用一个不支持的版本,即使这样做也会很困难。截至目前,我们在OpenJDK和甲骨文网站上没有找到任何Java 9或Java 10的软件包。

Let’s see which other JDK packages exist in the “Main” repository:

让我们看看 “Main “存储库中还有哪些JDK包存在。

$ apt list openjdk*jdk

On Ubuntu 18.04 LTS, we can choose between the two current LTS Java versions:

在Ubuntu 18.04 LTS上,我们可以在当前两个LTS的Java版本中进行选择。

Listing... Done
openjdk-11-jdk/bionic-updates,bionic-security,now 10.0.2+13-1ubuntu0.18.04.2 amd64 [installed,automatic]
openjdk-8-jdk/bionic-updates,bionic-security 8u181-b13-0ubuntu0.18.04.1 amd64

It’s also worth noting that although the package is called openjdk-11-jdk, as of this writing, it actually installs version 10.0.2. This is likely to change soon. We can see that if we inspect the package:

还值得注意的是,尽管该软件包被称为openjdk-11-jdk,截至本文写作时,它实际上安装了10.0.2版本。这可能很快就会改变。我们可以看到,如果我们检查该软件包。

$ apt show openjdk-11-jdk

Let’s have a look at the “Depends” section of the output. Note that these packages (e.g. a JRE) also get installed alongside openjdk-11-jdk:

让我们看看输出中的 “依赖 “部分。注意,这些包(例如JRE)也会与openjdk-11-jdk一起被安装。

Depends: openjdk-11-jre (= 10.0.2+13-1ubuntu0.18.04.2),
openjdk-11-jdk-headless (= 10.0.2+13-1ubuntu0.18.04.2),
libc6 (>= 2.2.5)

Let’s explore which other packages we have at our disposal besides the default jdk package:

让我们探索一下,除了默认的jdk包,我们还有哪些包可以使用。

$ apt list openjdk-11*
Listing... Done
openjdk-11-dbg/bionic-updates,bionic-security 10.0.2+13-1ubuntu0.18.04.2 amd64
openjdk-11-demo/bionic-updates,bionic-security 10.0.2+13-1ubuntu0.18.04.2 amd64
openjdk-11-doc/bionic-updates,bionic-updates,bionic-security,bionic-security 10.0.2+13-1ubuntu0.18.04.2 all
openjdk-11-jdk/bionic-updates,bionic-security 10.0.2+13-1ubuntu0.18.04.2 amd64
openjdk-11-jdk-headless/bionic-updates,bionic-security 10.0.2+13-1ubuntu0.18.04.2 amd64
openjdk-11-jre/bionic-updates,bionic-security,now 10.0.2+13-1ubuntu0.18.04.2 amd64 [installed,automatic]
openjdk-11-jre-headless/bionic-updates,bionic-security,now 10.0.2+13-1ubuntu0.18.04.2 amd64 [installed,automatic]
openjdk-11-jre-zero/bionic-updates,bionic-security 10.0.2+13-1ubuntu0.18.04.2 amd64
openjdk-11-source/bionic-updates,bionic-updates,bionic-security,bionic-security 10.0.2+13-1ubuntu0.18.04.2 all

We may find some of these packages useful. For example, openjdk-11-source contains source files for the classes of the Java core API, while openjdk-11-dbg contains the debugging symbols.

我们可能会发现这些包中有一些是有用的。例如,openjdk-11-source包含Java核心API的类的源文件,而openjdk-11-dbg包含调试符号。

Besides the openjdk-* family, there’s the default-jdk package, that is worth exploring:

除了openjdk-*系列,还有default-jdk包,这值得探索:

$ apt show default-jdk

At the end of the output, the description says:

在输出的最后,描述说。

“This dependency package points to the Java runtime, or Java compatible development kit recommended for this architecture…”

“这个依赖包指向Java运行时,或为这个架构推荐的Java兼容开发包……”

In the case of Ubuntu 18.04 LTS, it’s the package openjdk-11-jdk at the moment.

就Ubuntu 18.04 LTS而言,目前是openjdk-11-jdk包。

8. Overview: Java Versions and Packages

8.概述 Java版本和软件包

Now, let’s have a look at how different versions of Java could be installed on Ubuntu 18.04 LTS as of this writing:

现在,让我们来看看截至目前,不同版本的Java如何安装在Ubuntu 18.04 LTS上。

Version OpenJDK Oracle Java
11 manual installation manual installation
oracle-java11-installer in the linuxuprising PPA
10 manual installation – not supported manual installation – not supported
9 manual installation – not supported manual installation – not supported
8 openjdk-8-jdk in the “Main” repository oracle-java8-installer in the webupd8team PPA

9. Multiple Java Versions on an Ubuntu System

9.在Ubuntu系统上使用多个Java版本

The standard way for managing multiple versions of the same software on Ubuntu is via the Debian Alternatives System. Most of the time we create, maintain and display alternatives via the update-alternatives program.

在Ubuntu上管理同一软件的多个版本的标准方法是通过Debian Alternatives系统。大多数时候,我们通过update-alternatives程序创建、维护和显示替代品。

When apt installs a JDK package, it automatically adds the entries for the alternatives. In the case of manual installation, we’ve seen how to add the alternatives for java and javac respectively.

apt安装JDK包时,它会自动添加替代品的条目。在手动安装的情况下,我们已经看到如何分别为javajavac添加替代品。

Let’s have a look at our alternatives:

让我们来看看我们的替代品。

$ update-alternatives --display java

On our test system, where we’ve installed two different versions of OpenJDK, the output lists both alternatives with their respective priorities:

在我们的测试系统中,我们安装了两个不同版本的OpenJDK,输出结果列出了两个备选方案及其各自的优先级。

java - auto mode
link best version is /usr/lib/jvm/java-11-openjdk-amd64/bin/java
link currently points to /usr/lib/jvm/java-11-openjdk-amd64/bin/java
link java is /usr/bin/java
slave java.1.gz is /usr/share/man/man1/java.1.gz
/usr/lib/jvm/java-11-openjdk-amd64/bin/java - priority 1101
slave java.1.gz: /usr/lib/jvm/java-11-openjdk-amd64/man/man1/java.1.gz
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java - priority 1081
slave java.1.gz: /usr/lib/jvm/java-8-openjdk-amd64/jre/man/man1/java.1.gz

Now that we’ve seen our alternatives, we can also switch between them:

现在我们已经看到了我们的替代品,我们也可以在它们之间进行切换:

$ sudo update-alternatives --config java

Additionally, we get an interactive output, where we can switch between the alternatives via the keyboard:

此外,我们得到了一个交互式输出,我们可以通过键盘在备选方案之间切换。

There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1101 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1101 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:

If we’re working on multiple applications written in different versions of Java, chances are we’ll also need different versions of other software (e.g. Maven, some application server). In that case, we may want to consider using greater abstractions such as Docker containers.

如果我们正在开发用不同版本的Java编写的多个应用程序,有可能我们还需要不同版本的其他软件(例如Maven、一些应用服务器)。在这种情况下,我们可能要考虑使用更大的抽象,如Docker容器。

10. Conclusion

10.结论

To summarize, in this article, we’ve seen examples of installing a JDK from the “Main” repository, from a PPA, and manually. We’ve briefly compared these three installation methods.

总结一下,在这篇文章中,我们看到了从 “主 “仓库、从PPA和手动安装JDK的例子。我们简要地比较了这三种安装方法。

And finally, we’ve seen how to manage multiple Java installations on Ubuntu system with update-alternatives.

最后,我们已经看到了如何用update-alternatives来管理Ubuntu系统上的多个Java安装。

As a next step, it may be useful to set the JAVA_HOME environment variable.

作为下一步,设置JAVA_HOME环境变量可能是有用的。