How To Install Java with Apt on Debian 9

Introduction

Java and the JVM (Java's virtual machine) are required for many kinds of software, including Tomcat, Jetty, Glassfish, Cassandra and Jenkins.

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.

Prerequisites

To follow this tutorial, you will need:

Installing the Default JRE/JDK

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

This package will install OpenJDK version 1.8, which is compatible with Java 8. Java 8 is the current Long Term Support version and is still widely supported, though public maintenance ends in January 2019.

To install this version, first update the package index:

sudo apt update

Next, check if Java is already installed:

java -version

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

Output-bash: java: command not found

Execute the following command to install OpenJDK:

sudo apt install default-jre

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

Verify the installation with:

java -version

You'll see the following output:

Outputopenjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-1~deb9u1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

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:

sudo apt install default-jdk

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

javac -version

You'll see the following output:

Outputjavac 1.8.0_181

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

Installing the Oracle JDK

If you want to install the Oracle JDK, which is the official version distributed by Oracle, you'll need to add a new package repository for the version you'd like to use.

First, install the software-properties-common package which adds the apt-get-repository command which you'll use to add additional repositories to your sources list.

Install software-properties-common with:

sudo apt install software-properties-common

With this installed, you can install Oracle's Java.

Installing Oracle Java 8

To install Java 8, which is the current long-term support version, first add its package repository:

sudo add-apt-repository ppa:webupd8team/java

When you add the repository, you'll see a message like this:

output Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK8). There are no actual Java files in this PPA.

Important -> Why Oracle Java 7 And 6 Installers No Longer Work: http://www.webupd8.org/2017/06/why-oracle-java-7-and-6-installers-no.html

Update: Oracle Java 9 has reached end of life: http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html

The PPA supports Ubuntu 18.04, 17.10, 16.04, 14.04 and 12.04.

More info (and Ubuntu installation instructions):
- for Oracle Java 8: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html

Debian installation instructions:
- Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html

For Oracle Java 10, see a different PPA: https://www.linuxuprising.com/2018/04/install-oracle-java-10-in-ubuntu-or.html
 More info: https://launchpad.net/~webupd8team/+archive/ubuntu/java
Press [ENTER] to continue or ctrl-c to cancel adding it

Press ENTER to continue. It will attempt to import some GPG signing keys, but it won't be able to find any valid ones:

Outputgpg: keybox '/tmp/tmpgt9wdvth/pubring.gpg' created
gpg: /tmp/tmpgt9wdvth/trustdb.gpg: trustdb created
gpg: key C2518248EEA14886: public key "Launchpad VLC" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1
gpg: no valid OpenPGP data found.

Execute the following command to add the GPG key for the repository source manually:

apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C2518248EEA14886

Then update your package list:

sudo apt update

Once the package list updates, install Java 8:

sudo apt install oracle-java8-installer

Your system will download the JDK from Oracle and ask you to accept the license agreement. Accept the agreement and the JDK will install.

Installing Oracle Java 10

To install Oracle Java 10, first add its repository:

sudo add-apt-repository ppa:linuxuprising/java

You'll see this message:

Output Oracle Java 10 installer

Java binaries are not hosted in this PPA due to licensing. The packages in this PPA download and install Oracle Java 10 (JDK 10), 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/

Issues or suggestions? Leave a comment here: https://www.linuxuprising.com/2018/04/install-oracle-java-10-in-ubuntu-or.html
 More info: https://launchpad.net/~linuxuprising/+archive/ubuntu/java
Press [ENTER] to continue or ctrl-c to cancel adding it

Press ENTER to continue the installation. Like with Java 8, you'll see a message about invalid signing keys:

Outputgpg: keybox '/tmp/tmpvuqsh9ui/pubring.gpg' created
gpg: /tmp/tmpvuqsh9ui/trustdb.gpg: trustdb created
gpg: key EA8CACC073C3DB2A: public key "Launchpad PPA for Linux Uprising" imported
gpg: Total number processed: 1
gpg:               imported: 1
gpg: no valid OpenPGP data found.

Execute this command to import the necessary key:

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

Then update your package list:

sudo apt update

Once the package list updates, install Java 10:

sudo apt install oracle-java10-installer

Your system will download the JDK from Oracle and ask you to accept the license agreement. Accept the agreement and the JDK will install.

Now let's look at how to select which version of Java you want to use.

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.

sudo update-alternatives --config java

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

OutputThere are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-10-oracle/bin/java             1091      auto mode
* 1            /usr/lib/jvm/java-10-oracle/bin/java             1091      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  3            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      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.

You can do this for other Java commands, such as the compiler (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.

Let's set the JAVA_HOME environment variable next.

Setting the JAVA_HOME Environment Variable

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

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

sudo update-alternatives --config java

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

Output  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-10-oracle/bin/java             1091      auto mode
* 1            /usr/lib/jvm/java-10-oracle/bin/java             1091      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  3            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      manual mode

In this case the installation paths are as follows:

  • Oracle Java 10 is located at /usr/lib/jvm/java-10-oracle/jre/bin/java.
  • Oracle Java 8 is located at /usr/lib/jvm/java-8-oracle/jre/bin/java.
  • OpenJDK 8 is located at /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java.

These paths show the path to the java executable.

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

sudo 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:

/etc/environment

JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre"

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

Save the file and exit the editor.

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

source /etc/environment

Verify that the environment variable is set:

echo $JAVA_HOME

You'll see the path you just set:

Output/usr/lib/jvm/java-8-oracle/jre

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

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.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容