如何从 Linux 发行版的仓库中安装 Java
Java

> 
> 无论你喜欢哪个发行版和包管理器，都可以都很容易地在你的 Linux 系统上安装 Java。
> 
> 
> 


![](/data/attachment/album/202110/09/092717ean17be0alz10kt5.jpg "Coffee beans and a cup of coffee")


把 Java 安装到你的 Linux 桌面上有多种方法。一个明显的方式是使用你的 Linux 发行版中提供的软件包。请注意，这并不适合所有人。例如，有些用户可能需要一个非常具体的 Java 版本。


在你开始之前，你必须确定你需要“哪种 Java”。你是否只需要运行一些 `.class` 文件或 `.jar` 文件？还是你正在编写一些需要编译的代码？


就我而言，我运行的大部分 Java 都是我自己（至少部分）编写的 Java，所以安装完整的 Java 开发工具包（或称 JDK）总是有意义的，它包含了 Java 编译器、库和一些非常有用的工具。当然，在这里，我们更倾向于使用开源的 JDK，称为 [OpenJDK](https://openjdk.java.net/)。


由于我主要在 Ubuntu Linux 发行版上工作，我的软件包管理器是 `apt`。我可以用 `apt` 来查找哪些 OpenJDK 包是可用的：



```
apt list OpenJDK\*

```

这个命令的输出看起来像这样：



```
Listing... Done
openjdk-11-dbg/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-dbg/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-demo/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-demo/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-doc/hirsute-updates,hirsute-updates,hirsute-security,hirsute-security 11.0.11+9-0ubuntu2 all
openjdk-11-jdk-headless/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-jdk-headless/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-jdk/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-jdk/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-jre-dcevm/hirsute 11.0.10+1-1 amd64
openjdk-11-jre-headless/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-jre-headless/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-jre-zero/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-jre-zero/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-jre/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-jre/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-source/hirsute-updates,hirsute-updates,hirsute-security,hirsute-security 11.0.11+9-0ubuntu2 all
openjdk-15-dbg/hirsute 15.0.3+3-1 amd64
openjdk-15-dbg/hirsute 15.0.3+3-1 i386
openjdk-15-demo/hirsute 15.0.3+3-1 amd64
...
openjdk-8-jre/hirsute-updates,hirsute-security 8u292-b10-0ubuntu1 i386
openjdk-8-source/hirsute-updates,hirsute-updates,hirsute-security,hirsute-security 8u292-b10-0ubuntu1 all

```

我在上面用 `...` 省略了不少行。


事实证明，即使限制在 OpenJDK 中，我仍然有很多选择：


* 不同的架构（在我的例子中，i386 还是 amd64）。
* 不同的 Java 版本（就我而言，有 8、11、15、16、17 等）。
* 纯粹的 OpenJDK 或无头版本。
* Java 运行时环境（JRE）。
* 用于调试、演示，以及是否包含源代码等。


同样，在我的情况中，我主要对纯粹的普通 OpenJDK 感兴趣。


假设我想为我的 amd64 架构安装 Java 11 版本的普通 OpenJDK，我可以输入：



```
sudo apt install -a=amd64 openjdk-11-jdk

```

几分钟后，我就可以编译、运行、调试和打包我的 Java 代码了。


注意，很有可能需要同时安装多个版本的 Java，有时甚至是必要的。在 Ubuntu 中，有一个有用的工具，叫做 `update-java-alternatives`，它可以 [显示并配置在使用哪个 Java 环境](http://manpages.ubuntu.com/manpages/hirsute/man8/update-java-alternatives.8.html)。


那些使用不同 Linux 发行版的人，一般来说，可以采取类似的方法。其他的几个发行版（如 Debian 和 Mint）也使用 `apt` ，尽管可用的软件包可能不同。发行版可能使用不同的软件包管理器。例如， [Fedora 安装 Java 的文档页面](https://docs.fedoraproject.org/en-US/quick-docs/installing-java/) 显示了如何使用 Fedora `dnf` 包管理器来处理安装。首先，为了显示可用的版本，输入：



```
dnf search openjdk

```

接下来，要安装完整的开发 x86\_64 架构版本，请输入：



```
sudo dnf install java-11-openjdk-devel.x86_64

```

同样地，Fedora 提供了 `alternatives` 工具来 [显示和配置 Java 环境](https://tecadmin.net/install-java-on-fedora/)。


再比如，[很棒的 Arch Linux 维基](https://wiki.archlinux.org/title/java#OpenJDK) 显示对应的软件包是 `jdk11-openjdk`。该维基还解释了许多在 Arch 中使用 Java 的其他重要细节，比如使用 `archlinux-java` 工具来显示安装了哪些 Java 环境或选择一个不同的默认环境。Arch 使用一个叫 `pacman` 的包管理器，它也有文档 [在 Arch Linux 维基上](https://wiki.archlinux.org/title/pacman#Querying_package_databases)。


不管你喜欢哪个发行版和软件包管理器，在你的 Linux 系统上获得 Java 是很容易的。当然，在安装之前，要考虑版本和功能。还要记住，在同一台电脑上有管理两个或多个 Java 版本的方法。我的大多数例子都使用了 `apt`，但也要记得可以选择使用 `dnf`。




---


via: <https://opensource.com/article/21/9/install-java-linux-repositories>


作者：[Chris Hermansen](https://opensource.com/users/clhermansen) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
There are a number of different ways to install Java on your Linux desktop. An obvious route is to use the packages provided in your Linux distribution. Note that this doesn’t work for everyone; for example, some users may need a very specific version of Java.

Before you can start, you must determine “which Java” you need. Do you just need to run some `.class`

files or a `.jar`

file? Or are you writing some code that you need to compile?

In my case, most of the Java I run is Java that I have (at least partly) written myself, so it always makes sense to install the full Java Development Kit, or JDK, which comes with a Java compiler, libraries, and some really useful utilities. And of course, hereabouts, we give preference to the open source JDK, called [OpenJDK](https://openjdk.java.net/).

Since I primarily work on the Ubuntu Linux distribution, my package manager is `apt`

. I can use `apt`

to find out which OpenJDK packages are available:

`apt list OpenJDK\*`


The output of this command looks like:

```
Listing... Done
openjdk-11-dbg/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-dbg/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-demo/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-demo/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-doc/hirsute-updates,hirsute-updates,hirsute-security,hirsute-security 11.0.11+9-0ubuntu2 all
openjdk-11-jdk-headless/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-jdk-headless/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-jdk/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-jdk/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-jre-dcevm/hirsute 11.0.10+1-1 amd64
openjdk-11-jre-headless/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-jre-headless/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-jre-zero/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-jre-zero/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-jre/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 amd64
openjdk-11-jre/hirsute-updates,hirsute-security 11.0.11+9-0ubuntu2 i386
openjdk-11-source/hirsute-updates,hirsute-updates,hirsute-security,hirsute-security 11.0.11+9-0ubuntu2 all
openjdk-15-dbg/hirsute 15.0.3+3-1 amd64
openjdk-15-dbg/hirsute 15.0.3+3-1 i386
openjdk-15-demo/hirsute 15.0.3+3-1 amd64
...
openjdk-8-jre/hirsute-updates,hirsute-security 8u292-b10-0ubuntu1 i386
openjdk-8-source/hirsute-updates,hirsute-updates,hirsute-security,hirsute-security 8u292-b10-0ubuntu1 all
```


I have elided quite a few lines above with `...`

.

It turns out that, even though I’m limiting myself to the OpenJDK, I still have plenty of options for:

- The architecture (in my case, whether i386 or amd64).
- The version of Java (in my case, whether 8, 11, 15, 16, 17).
- The pure OpenJDK or the headless version.
- The Java Runtime Environment or JRE.
- Options for debugging, demo, source code, etc.

Again, in my case, primarily, I’m just interested in the pure vanilla OpenJDK.

Supposing then I want to install the Java 11 version of the plain vanilla OpenJDK for my amd64 architecture, I can type:

`sudo apt install -a=amd64 openjdk-11-jdk`



A few minutes later, I’ll be able to compile, run, debug, and package my Java code.

Note that it’s quite possible, and sometimes even desirable, to have multiple versions of Java installed simultaneously. In the case of Ubuntu, there is a useful utility called `update-java-alternatives`

that [displays and configures which java environment](http://manpages.ubuntu.com/manpages/hirsute/man8/update-java-alternatives.8.html) to use at any point in time.

Those of you using a different Linux distro can, in general, take a similar approach. Several other distros (such as Debian and Mint) also use `apt`

, though the available packages may differ. Other distros use different package managers. For example, [the Fedora documentation page for Installing Java](https://docs.fedoraproject.org/en-US/quick-docs/installing-java/) shows how to use the Fedora `dnf`

package manager to handle the installation. First, to show the available version, type:

`dnf search openjdk`


Next, to install the full development x86_64 architecture version, type:

`sudo dnf install java-11-openjdk-devel.x86_64`


Similarly, Fedora provides the `alternatives`

utility to [display and configure the Java environment](https://tecadmin.net/install-java-on-fedora/).

For another example, the [wonderful Arch Linux wiki](https://wiki.archlinux.org/title/java#OpenJDK) shows the corresponding package is `jdk11-openjdk`

**. **The wiki also explains many other important details related to using Java in Arch, such as using the `archlinux-java`

utility to show what Java environments are installed or select a different default environment. Arch uses a package manager called `pacman`

, which is also documented [here on the Arch Linux wiki](https://wiki.archlinux.org/title/pacman#Querying_package_databases).

Regardless of which distribution and package manager you prefer, it is easy to get Java on your Linux system. Consider version and function, of course, before installing. Also keep in mind that there are options for managing two or more Java versions on the same computer. Most of my examples used `apt`

, but keep in mind the `dnf`

options, too.

## Comments are closed.