如何在Debian 10上使用Apt安装Java

介绍 (Introduction)

Java and the JVM (Java Virtual Machine) are required for many kinds of software, including Tomcat, Jetty, Glassfish, Cassandra and Jenkins.

Java和JVM(Java虚拟机)是许多软件所必需的,包括TomcatJettyGlassfishCassandraJenkins

In this guide, you will install different versions of the Java Runtime Environment (JRE) and the Java Developer Kit (JDK) using Debian’s apt package management system.

在本指南中,您将使用Debian的apt软件包管理系统安装不同版本的Java Runtime Environment(JRE)和Java Developer Kit(JDK)。

You’ll install OpenJDK 11 as well as the official Java 11 software from Oracle. You’ll then select the version you wish to use for your projects. When you’re finished, you’ll be able to use the JDK to develop software or use the Java Runtime to run software.

您将安装OpenJDK 11以及Oracle的官方Java 11软件。 然后,选择要用于项目的版本。 完成后,您将可以使用JDK开发软件或使用Java Runtime运行软件。

先决条件 (Prerequisites)

To begin this tutorial, you will need:

要开始本教程,您将需要:

安装默认的JRE / JDK (Installing the Default JRE/JDK)

The easiest option for installing Java is to use the version packaged with Debian. By default, Debian 10 includes OpenJDK version 11, which is an open-source variant of the JRE and JDK, and is compatible with Java 11.

安装Java的最简单选择是使用Debian附带的版本。 默认情况下,Debian 10包含OpenJDK版本11,它是JRE和JDK的开源变体,并且与Java 11兼容。

Java 11 is the current Long Term Support version of Java.

Java 11是Java的当前长期支持版本。

To install the OpenJDK version of Java, first update your apt package index:

要安装Java的OpenJDK版本,请首先更新您的apt软件包索引:

  • sudo apt update

    sudo apt更新

Next, check if Java is already installed:

接下来,检查是否已安装Java:

  • java -version

    Java版本

If Java is not currently installed, you’ll see the following output:

如果当前未安装Java,则会看到以下输出:


   
   
Output
-bash: java: command not found

Then use the apt install command to install OpenJDK:

然后使用apt install命令安装OpenJDK:

  • sudo apt install default-jre

    须藤apt install default-jre

This will install the Java Runtime Environment (JRE), allowing you to run almost all Java software.

这将安装Java Runtime Environment(JRE),使您可以运行几乎所有Java软件。

Verify the installation with:

使用以下命令验证安装:

  • java -version

    Java版本

You’ll see the following output:

您将看到以下输出:


   
   
Output
openjdk version "11.0.4" 2019-07-16 OpenJDK Runtime Environment (build 11.0.4+11-post-Debian-1deb10u1) OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Debian-1deb10u1, mixed mode, sharing)

You may also need the Java Development Kit (JDK) in order to compile and run some specific Java-based software. To install the JDK, execute the following command:

您可能还需要Java开发工具包(JDK)来编译和运行某些基于Java的特定软件。 要安装JDK,请执行以下命令:

  • sudo apt install default-jdk

    须藤apt install default-jdk

Verify that the JDK is installed by checking the version of javac, the Java compiler:

通过检查Java编译器javac的版本来验证是否已安装JDK:

  • javac -version

    javac-版本

You’ll see the following output:

您将看到以下输出:


   
   
Output
javac 11.0.4

Next, let’s look at how to install Oracle’s official JDK and JRE.

接下来,让我们看看如何安装Oracle的官方JDK和JRE。

安装Oracle JDK (Installing the Oracle JDK)

To install the official Oracle JDK, we’ll need to download some files directly from Oracle, then install them using an installer we’ll fetch from a third-party repository.

要安装官方的Oracle JDK,我们需要直接从Oracle下载一些文件,然后使用将从第三方存储库中获取的安装程序进行安装。

First, let’s download Java from Oracle’s website.

首先,让我们从Oracle网站下载Java。

下载Oracle的Java SE软件包 (Downloading Oracle’s Java SE Software Package)

Due to recent changes in the way Oracle handles Java licensing, you’ll need to create an Oracle account and download the software directly from their website before continuing with the installation.

由于Oracle处理Java许可方式的最新变化,您需要先创建一个Oracle帐户并直接从其网站下载软件,然后再继续安装。

If you don’t already have an Oracle account, create one at Oracle’s account creation page.

如果您还没有Oracle帐户,请在Oracle的帐户创建页面上创建一个。

Afterwards, navigate to Oracle’s Java SE Downloads page:

然后,导航至Oracle的Java SE下载页面

We need to make sure we’re downloading the correct version of Java, because it needs to match what the installer is expecting. We can check what version the installer needs by visiting the package listing of the installer’s software repository.

我们需要确保下载的是正确版本的Java,因为它需要与安装程序的期望相匹配。 我们可以通过访问安装程序软件存储库的软件包清单来检查安装程序需要哪个版本。

We’ll download the install from this repository in the next step, but for now look for the oracle-java11-installer-local... files:

下一步,我们将从此存储库下载安装,但现在查找oracle-java11-installer-local...文件:

In this case, we can see that the installer is expecting version 11.0.4. Ignore any number that comes after the - hyphen in the package version number (1 in the example screenshot above).

在这种情况下,我们可以看到安装程序期望版本为11.0.4。 忽略软件包版本号中-连字符后面的任何数字(在上面的示例屏幕快照中为1 )。

Now that we know the correct version number, scroll down the Java download page until you find the correct version:

现在我们知道正确的版本号,向下滚动Java下载页面,直到找到正确的版本:

Press the download button, and you’ll be taken to one final screen:

按下下载按钮,您将进入一个最终屏幕:

Select the Accept License Agreement radio button, then click the .tar.gz package for Linux, and your download will begin. You may need to log in to your Oracle account one more time before the download starts.

选择“ 接受许可协议”单选按钮,然后单击Linux的.tar.gz软件包,然后将开始下载。 在开始下载之前,您可能需要再登录一次Oracle帐户。

After the download has finished, we need to get the downloaded file onto our server. We will use the scp command to do so, but you could use any file transfer software you’re comfortable with.

下载完成后,我们需要将下载的文件下载到我们的服务器上。 我们将使用scp命令来执行此操作,但是您可以使用自己喜欢的任何文件传输软件。

On your local computer, use scp to upload the file to your server. The following command assumes your file downloaded to a Downloads directory in your user’s home folder, and will upload to the sammy user’s home directory on the server. Adjust the file paths as needed for your systems:

在本地计算机上,使用scp将文件上传到服务器。 以下命令假定您的文件已下载到用户主文件夹中的“ 下载”目录中,并将上传到服务器上的sammy用户的主目录中。 根据系统需要调整文件路径:

  • scp ~/Downloads/jdk-11.0.4_linux-x64_bin.tar.gz sammy@your_server_ip:~

    scp〜/ Downloads / jdk- 11.0.4 _linux-x64_bin.tar.gz sammy @ your_server_ip :〜

Now that we’ve got the correct Java software package up on our server, let’s add the repo that contains the installer we’ll use to install Java.

现在,我们已经在服务器上安装了正确的Java软件包,让我们添加包含用于安装Java的安装程序的存储库。

添加安装程序的Apt存储库 (Adding the Installer’s Apt Repository)

First, install the software-properties-common package, which adds the add-apt-repository command to your system:

首先,安装software-properties-common软件包,该软件包将add-apt-repository命令add-apt-repository到您的系统中:

  • sudo apt install software-properties-common

    sudo apt安装软件属性通用

Next, import the signing key used to verify the software we’re about to install:

接下来,导入用于验证我们将要安装的软件的签名密钥:

  • sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A

    须藤apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A

Then we use the add-apt-repository command to add the repo to our list of package sources:

然后,我们使用add-apt-repository命令将add-apt-repository添加到我们的软件包源列表中:

  • sudo add-apt-repository ppa:linuxuprising/java

    sudo add-apt-repository ppa:linuxuprising / java

You’ll see this message:

您会看到以下消息:


   
   
Output
Oracle Java 11 (LTS) and 12 installer for Ubuntu, Linux Mint and Debian. Java binaries are not hosted in this PPA due to licensing. The packages in this PPA download and install Oracle Java 11, so a working Internet connection is required. The packages in this PPA are based on the WebUpd8 Oracle Java PPA packages: https://launchpad.net/~webupd8team/+archive/ubuntu/java Created for users of https://www.linuxuprising.com/ Installation instructions (with some tips), feedback, suggestions, bug reports etc.: . . . Press [ENTER] to continue or ctrl-c to cancel adding it

Press ENTER to continue the installation. You’ll may see a message about no valid OpenPGP data found. This can be ignored.

ENTER继续安装。 您可能会看到一条消息,提示no valid OpenPGP data found 。 这可以忽略。

Finally, update your package list to make the new software available for installation:

最后,更新您的软件包列表,以使新软件可用于安装:

  • sudo apt update

    sudo apt更新

Next we’ll, install the Java package using the new software we just made available.

接下来,我们将使用刚刚提供的新软件来安装Java软件包。

安装下载的Java软件 (Installing the Downloaded Java Software)

First, we need to create a specific directory that the installer uses to find the Java software package, then copy the .tar.gz file in:

首先,我们需要创建安装程序用来查找Java软件包的特定目录,然后将.tar.gz文件复制到:

  • sudo mkdir -p /var/cache/oracle-jdk11-installer-local/

    须藤mkdir -p / var / cache / oracle-jdk11-installer-local /
  • sudo cp ~/jdk-11.0.4_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/

    须藤cp〜 / jdk- 11.0.4 _linux-x64_bin.tar.gz / var / cache / oracle-jdk11-installer-local /

Once the file copy is complete, install Oracle’s Java 11 by installing the oracle-java11-installer-local package:

文件复制完成后,通过安装oracle-java11-installer-local软件包来安装Oracle的Java 11:

  • sudo apt install oracle-java11-installer-local

    须藤apt install oracle-java11-installer-local

The installer will first ask you to accept the Oracle license agreement. Accept the agreement, then the installer will extract the Java package and install it.

安装程序将首先要求您接受Oracle许可协议。 接受该协议,然后安装程序将提取Java软件包并进行安装。

Now that we have multiple versions of Java installed, let’s look at how to select which version of Java you want to use.

现在我们已经安装了多个Java版本,让我们看看如何选择要使用的Java版本。

管理Java (Managing Java)

You can have multiple Java installations on one server. You can configure which version is the default for use on the command line by using the update-alternatives command.

一台服务器上可以安装多个Java。 您可以使用update-alternatives命令配置哪个版本是在命令行上使用的默认版本。

  • sudo update-alternatives --config java

    sudo update-alternatives --config Java

This is what the output would look like if you’ve installed both versions of Java in this tutorial:

如果您在本教程中安装了两个版本的Java,则输出结果如下所示:


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

Choose the number associated with the Java version to use it as the default, or press ENTER to leave the current settings in place.

选择与Java版本关联的数字以将其用作默认值,或按ENTER以保留当前设置。

You can do this for other Java commands, such as the compiler (javac):

您可以对其他Java命令执行此操作,例如编译器( javac ):

  • sudo update-alternatives --config javac

    sudo update-alternatives --config javac

Other commands for which this command can be run include, but are not limited to: keytool, javadoc, and jarsigner.

可以运行此命令的其他命令包括但不限于: keytooljavadocjarsigner

Let’s set the JAVA_HOME environment variable next.

接下来让我们设置JAVA_HOME环境变量。

设置JAVA_HOME环境变量 (Setting the JAVA_HOME Environment Variable)

Many programs written in Java use the JAVA_HOME environment variable to determine which Java installation location to use.

许多用Java编写的程序都使用JAVA_HOME环境变量来确定要使用的Java安装位置。

To set this environment variable, first determine where Java is installed. Use the update-alternatives command again:

要设置此环境变量,首先确定Java的安装位置。 再次使用update-alternatives命令:

  • sudo update-alternatives --config java

    sudo update-alternatives --config Java

This command shows each installation of Java along with its installation path:

此命令显示Java的每次安装及其安装路径:


   
   
Output
Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode * 2 /usr/lib/jvm/java-11-oracle/bin/java 1091 manual mode

In this case the installation paths are as follows:

在这种情况下,安装路径如下:

  • Oracle Java 11 is located at /usr/lib/jvm/java-11-oracle/bin/java.

    Oracle Java 11位于/usr/lib/jvm/java-11-oracle/bin/java

  • OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java.

    OpenJDK 11位于/usr/lib/jvm/java-11-openjdk-amd64/bin/java

These paths show the path to the java executable.

这些路径显示了java可执行文件的路径。

Copy the path for your preferred installation, excluding the trailing bin/java component. Then open /etc/environment using nano or your favorite text editor:

复制首选安装的路径,但不包括结尾的bin/java组件。 然后使用nano或您喜欢的文本编辑器打开/etc/environment

  • sudo nano /etc/environment

    须藤nano / etc / environment

This file may be blank initially. At the end of the file, add the following line, making sure to replace the highlighted path with your own copied path:

此文件最初可能是空白的。 在文件末尾,添加以下行,确保将突出显示的路径替换为您自己的复制路径:

/etc/environment
/ etc /环境
JAVA_HOME="/usr/lib/jvm/java-11-oracle/"

Modifying this file will set the JAVA_HOME path for all users on your system.

修改此文件将为系统上的所有用户设置JAVA_HOME路径。

Save the file and exit the editor.

保存文件并退出编辑器。

Now reload this file to apply the changes to your current session:

现在,重新加载此文件以将更改应用于当前会话:

  • source /etc/environment

    来源/ etc / environment

Verify that the environment variable is set:

验证是否设置了环境变量:

  • echo $JAVA_HOME

    回声$ JAVA_HOME

You’ll see the path you just set:

您会看到刚刚设置的路径:


   
   
Output
/usr/lib/jvm/java-11-oracle/

Other users will need to execute the command source /etc/environment or log out and log back in to apply this setting.

其他用户将需要执行命令source /etc/environment或注销并重新登录以应用此设置。

结论 (Conclusion)

In this tutorial you installed multiple versions of Java and learned how to manage them. You can now install software which runs on Java, such as Tomcat, Jetty, Glassfish, Cassandra or Jenkins.

在本教程中,您安装了Java的多个版本,并学习了如何管理它们。 现在,您可以安装在Java上运行的软件,例如Tomcat,Jetty,Glassfish,Cassandra或Jenkins。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-on-debian-10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值