博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在Ubuntu 18.04上使用Apt安装Java
阅读量:2506 次
发布时间:2019-05-11

本文共 12823 字,大约阅读时间需要 42 分钟。

The author selected the to receive a $100 donation as part of the program.

作者选择了作为计划的一部分,获得了100美元的捐款。

介绍 (Introduction)

Java and the JVM (Java’s virtual machine) are required for many kinds of software, including , , , and .

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

In this guide, you will install various versions of the Java Runtime Environment (JRE) and the Java Developer Kit (JDK) using apt . You’ll install OpenJDK as well as official packages 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.

在本指南中,您将使用apt安装各种版本的Java Runtime Environment(JRE)和Java Developer Kit(JDK)。 您将安装OpenJDK以及Oracle的官方软件包。 然后,选择要用于项目的版本。 完成后,您将可以使用JDK开发软件或使用Java Runtime运行软件。

先决条件 (Prerequisites)

To follow this tutorial, you will need:

要遵循本教程,您将需要:

  • One Ubuntu 18.04 server set up by following the tutorial, including a sudo non-root user and a firewall.

    通过遵循教程来一台Ubuntu 18.04服务器,包括sudo非root用户和防火墙。

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

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

安装Java的最简单选择是使用Ubuntu随附的版本。 默认情况下,Ubuntu 18.04包含OpenJDK版本11,这是JRE和JDK的开源变体。

To install this version, first update the package index:

要安装此版本,请首先更新软件包索引:

  • 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   
Command 'java' not found, but can be installed with:apt install default-jreapt install openjdk-11-jre-headlessapt install openjdk-8-jre-headless

Execute the following command to install the default Java Runtime Environment (JRE), which will install the JRE from OpenJDK 11:

执行以下命令以安装默认的Java Runtime Environment(JRE),它将从OpenJDK 11安装JRE:

  • sudo apt install default-jre

    须藤apt install default-jre

The JRE will allow you to run almost all Java software.

JRE将允许您运行几乎所有的Java软件。

Verify the installation with:

使用以下命令验证安装:

  • java -version

    Java版本

You’ll see the following output:

您将看到以下输出:

Output   
openjdk version "11.0.7" 2020-04-14OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-2ubuntu218.04)OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-2ubuntu218.04, mixed mode, sharing)

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

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

  • 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.7

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

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

安装Oracle JDK 11 (Installing Oracle JDK 11)

Oracle’s licensing agreement for Java doesn’t allow automatic installation through package managers. To install the Oracle JDK, which is the official version distributed by Oracle, you must create an Oracle account and manually download the JDK to add a new package repository for the version you’d like to use. Then you can use apt to install it with help from a .

Oracle的Java许可协议不允许通过程序包管理器自动安装。 要安装Oracle发行的正式版本的Oracle JDK,您必须创建一个Oracle帐户并手动下载JDK,以为要使用的版本添加新的软件包存储库。 然后,您可以使用apt在帮助下进行 。

The version of Oracle’s JDK you’ll need to download must match version of the installer script. To find out which version you need, visit the page.

您需要下载的Oracle JDK版本必须与安装脚本的版本匹配。 要查找所需的版本,请访问页面。

Locate the package for Bionic, as shown in the following figure:

找到Bionic的包装,如下图所示:

In this image, the version of the script is 11.0.7. In this case, you’ll need Oracle JDK 11.0.7. You don’t need to download anything from this page; you’ll download the installation script through apt shortly.

在此图像中,脚本的版本为11.0.7 。 在这种情况下,您将需要Oracle JDK 11.0.7。 您无需从此页面下载任何内容; 您将很快通过apt下载安装脚本。

Then visit the and locate the version that matches the one you need.

然后访问 ,找到与所需版本匹配的版本。

Click the JDK Download button and you’ll be taken to a screen that shows the versions available. Click the .tar.gz package for Linux.

单击“ JDK下载”按钮,您将进入显示可用版本的屏幕。 单击适用于Linux的.tar.gz软件包。

You’ll be presented with a screen asking you to accept the Oracle license agreement. Select the checkbox to accept the license agreement and press the Download button. Your download will begin. You may need to log in to your Oracle account one more time before the download starts.

将显示一个屏幕,要求您接受Oracle许可协议。 选中复选框以接受许可协议,然后按“ 下载”按钮。 您的下载将开始。 在开始下载之前,您可能需要再登录一次Oracle帐户。

Once the file has downloaded, you’ll need to transfer it to your server. On your local machine, upload the file to your server. On macOS, Linux, or Windows using the , use the scp command to transfer the file to the home directory of your sammy user. The following command assumes you’ve saved the Oracle JDK file to your local machine’s Downloads folder:

下载文件后,您需要将其传输到服务器。 在本地计算机上,将文件上传到服务器。 在macOS,Linux或使用的 ,使用scp命令将文件传输到sammy用户的主目录。 以下命令假定您已将Oracle JDK文件保存到本地计算机的“ Downloads文件夹中:

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

    scp下载/ jdk- 11.0.7 _linux-x64_bin.tar.gz sammy @ your_server_ip :〜

Once the file upload has completed, return to your server and add the third-party repository that will help you install Oracle’s Java.

文件上传完成后,返回服务器并添加第三方存储库,该存储库将帮助您安装Oracle的Java。

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 you’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

You’ll see this output:

您将看到以下输出:

Output   
gpg: key EA8CACC073C3DB2A: public key "Launchpad PPA for Linux Uprising" importedgpg: Total number processed: 1gpg: imported: 1

Then use the add-apt-repository command to add the repo to your 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/javaCreated 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 may see a message about no valid OpenPGP data found, but you can safely ignore this.

ENTER继续安装。 您可能会看到一条有关no valid OpenPGP data found的消息,但是您可以放心地忽略它。

Update your package list to make the new software available for installation:

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

  • sudo apt update

    sudo apt更新

The installer will look for the Oracle JDK you downloaded in /var/cache/oracle-jdk11-installer-local. Create this directory and move the Oracle JDK archive there:

安装程序将查找您在/var/cache/oracle-jdk11-installer-local下载的Oracle JDK。 创建此目录,然后将Oracle JDK存档移至该目录:

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

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

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

Finally, install the package:

最后,安装软件包:

  • 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 let’s look at how to select which version of Java you want to use.

现在让我们看看如何选择要使用的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

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

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

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

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

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

要设置此环境变量,首先确定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   
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 modePress
to keep the current choice[*], or type selection number:

In this case the installation paths are as follows:

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

  1. 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.

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

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

Copy the path from your preferred installation. Then open /etc/environment using nano or your favorite text editor:

从首选安装中复制路径。 然后使用nano或您喜欢的文本编辑器打开/etc/environment

  • sudo nano /etc/environment

    须藤nano / etc / environment

At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path, but do not include the /bin portion of the path:

在此文件的末尾,添加以下行,确保将突出显示的路径替换为您自己复制的路径,但不要包括该路径的/bin部分:

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

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-openjdk-amd64

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。

翻译自:

转载地址:http://wmhgb.baihongyu.com/

你可能感兴趣的文章
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
人工智能暑期课程实践项目——智能家居控制(一)
查看>>
前端数据可视化插件(二)图谱
查看>>
kafka web端管理工具 kafka-manager【转发】
查看>>
获取控制台窗口句柄GetConsoleWindow
查看>>
Linux下Qt+CUDA调试并运行
查看>>
51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
查看>>
OKMX6Q在ltib生成的rootfs基础上制作带QT库的根文件系统
查看>>
zabbix
查看>>
多线程基础
查看>>
完美解决 error C2220: warning treated as error - no ‘object’ file generated
查看>>
使用SQL*PLUS,构建完美excel或html输出
查看>>
SQL Server数据库笔记
查看>>
X-Forwarded-For伪造及防御
查看>>
android系统平台显示驱动开发简要:LCD驱动调试篇『四』
查看>>