如何在 Ubuntu 16.04 上使用 Apt-Get 安装 Java
Java 编程语言最初由 Sun Microsystems 开发,由 James Gosling 发起,于 1995 年作为 Sun Microsystems Java 平台(Java 1.0 [J2SE])的核心组件发布。
Java 标准版的最新版本是 Java SE 8。随着 Java 的发展和广泛普及,构建了多种配置以适应各种类型的平台。例如 - 用于企业应用程序的 J2EE,用于移动应用程序的 J2ME。
新的 J2 版本分别重命名为 Java SE、Java EE 和 Java ME。Java 保证是一次编写,随处运行。本文介绍了“如何在 Ubuntu 上使用 Apt-get 安装 Java”。
安装默认 JRE/JDK
要安装 Java 运行时环境 (JRE),请使用以下命令:
$ sudo apt-get install default-jre
示例输出应如下所示:
Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: apport-hooks-elementary contractor javascript-common libgda-5.0-4 libgda-5.0-common libgranite-common libgranite3 libgsignon-glib1 libindicate5 libjs-jquery libnoise-core0 libtagc0 mysql-server-5.7 mysql-server-core-5.7 Use 'sudo apt autoremove' to remove them. The following additional packages will be installed: ca-certificates-java default-jre-headless fonts-dejavu-extra java-common libbonobo2-0 libbonobo2-common libgif7 libgnome-2-0 libgnome2-common libgnomevfs2-0 libgnomevfs2-common liborbit-2-0 openjdk-8-jre openjdk-8-jre-headless Suggested packages: default-java-plugin libbonobo2-bin desktop-base libgnomevfs2-bin libgnomevfs2-extra gamin | fam gnome-mime-data icedtea-8-plugin openjdk-8-jre-jamvm fonts-ipafont-gothic fonts-ipafont-mincho ttf-wqy-microhei | ttf-wqy-zenhei fonts-indic The following NEW packages will be installed: ca-certificates-java default-jre default-jre-headless fonts-dejavu-extra java-common libbonobo2-0 libbonobo2-common libgif7 libgnome-2-0 ...........................................................................
还有另一个名为 JDK(Java 开发工具包)的默认 Java 安装。如果您要编译 Java 程序或将要使用的软件专门需要 JDK,则通常需要 JDK。要安装 JDK,请使用以下命令:
$ sudo apt-get install default-jdk
示例输出应如下所示:
Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: apport-hooks-elementary contractor javascript-common libgda-5.0-4 libgda-5.0-common libgranite-common libgranite3 libgsignon-glib1 libindicate5 libjs-jquery libnoise-core0 libtagc0 mysql-server-5.7 mysql-server-core-5.7 Use 'sudo apt autoremove' to remove them. The following additional packages will be installed: default-jdk-headless libice-dev libpthread-stubs0-dev libsm-dev libx11-dev libx11-doc libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-8-jdk openjdk-8-jdk-headless x11proto-core-dev x11proto-input-dev x11proto-kb-dev xorg-sgml-doctools xtrans-dev Suggested packages: libice-doc libsm-doc libxcb-doc libxt-doc openjdk-8-demo openjdk-8-source visualvm The following NEW packages will be installed: default-jdk default-jdk-headless libice-dev libpthread-stubs0-dev libsm-dev libx11-dev libx11-doc libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-8-jdk openjdk-8-jdk-headless x11proto-core-dev x11proto-input-dev x11proto-kb-dev xorg-sgml-doctools xtrans-dev 0 upgraded, 18 newly installed, 0 to remove and 168 not upgraded. Need to get 11.9 MB of archives. .....................................................................................
安装 Oracle JDK
要安装 Oracle JDK,请使用以下命令:
$ sudo add-apt-repository ppa:webupd8team/java
示例输出应如下所示:
Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK7 / JDK8 / JDK9). There are no actual Java files in this PPA. More info (and Ubuntu installation instructions): - for Oracle Java 7: http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html - for Oracle Java 8: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html Debian installation instructions: - Oracle Java 7: http://www.webupd8.org/2012/06/how-to-install-oracle-java-7-in-debian.html - Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html Important!!! For now, you should continue to use Java 8 because Oracle Java 9 is available as an early access release (it should be released in 2016)! You should only use Oracle Java 9 if you explicitly need it, because it may contain bugs and it might not include the latest security patches! Also, some Java options were removed in JDK9, so you may encounter issues with various Java apps. More information and installation instructions (Ubuntu / Linux Mint / Debian): http://www.webupd8.org/2015/02/install-oracle-java-9-in-ubuntu-linux.html More info: https://launchpad.net/~webupd8team/+archive/ubuntu/java Press [ENTER] to continue or ctrl-c to cancel adding it gpg: keyring `/tmp/tmpa5dj4h41/secring.gpg' created gpg: keyring `/tmp/tmpa5dj4h41/pubring.gpg' created gpg: requesting key EEA14886 from hkp server keyserver.ubuntu.com ..........................................................................................
现在,使用以下命令更新软件包列表:
$ sudo apt-get update
管理 Java
一台服务器上可能有多个 Java 安装。您可以配置一个特定的模型,该模型可以作为默认模型在命令行中使用,方法是使用如下所示的 update-options:
$ sudo update-alternatives --config java
示例输出应如下所示:
There are 5 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 auto mode 1 /usr/lib/jvm/java-6-oracle/jre/bin/java 1 manual mode 2 /usr/lib/jvm/java-7-oracle/jre/bin/java 2 manual mode 3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode 4 /usr/lib/jvm/java-8-oracle/jre/bin/java 3 manual mode 5 /usr/lib/jvm/java-9-oracle/bin/java 4 manual mode Press to keep the current choice[*], or type selection number:
您可以根据您的知识进行选择。
设置 JAVA_HOME 环境变量
要设置此环境变量,我们首先需要找出 Java 安装在哪里。您可以通过执行以下命令来做到这一点:
$ sudo update-alternatives --config java
复制您首选安装的路径,然后打开 /etc/environment 文件,如下所示:
$ sudo nano /etc/environment
在文件末尾添加以下行:
JAVA_HOME="YOUR_JAVA_PATH"
例如,它应如下所示:
JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java"
保存并退出文件,然后重新加载它,如下所示:
$ source /etc/environment
您现在可以通过执行以下命令来测试环境变量是否已设置:
$ echo $JAVA_HOME
阅读完本文后,您将能够理解:如何在 Ubuntu 16.04 上使用 Apt-Get 安装 Java。在我们的下一篇文章中,我们将提供更多基于 Linux 的技巧和提示。继续关注!