Install Java on Debian/Ubuntu

To install OpenJDK or Oracle JDK on Debian or Ubuntu, the most convenient way is to use their package manager called APT (Advanced Package Tool).

Install OpenJDK

The first step is to add the openjdk APT repository which is ppa:openjdk-r/ppa to APT source list. Here is the command for it (Note that you need to be sudo user):

$ sudo add-apt-repository ppa:openjdk-r/ppa

After that, update your APT local repository index using following command

$ sudo apt-get update

Now if you search for openjdk-12-jdk using apt search, you can find following items

$ sudo apt search openjdk-12-jdk
Sorting… Done
Full Text Search… Done
openjdk-12-jdk/xenial 12.0.1+12-4~16.04.1 amd64
  OpenJDK Development Kit (JDK)
openjdk-12-jdk-headless/xenial 12.0.1+12-4~16.04.1 amd64
  OpenJDK Development Kit (JDK) (headless)

Using apt install command you can install it

$ sudo apt install openjdk-12-jdk
 Reading package lists… Done
 Building dependency tree
 Reading state information… Done
 The following additional packages will be installed:
   openjdk-12-jdk-headless openjdk-12-jre openjdk-12-jre-headless
 Suggested packages:
   openjdk-12-demo openjdk-12-source visualvm libnss-mdns fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei | fonts-wqy-zenhei fonts-indic
 The following NEW packages will be installed:
   openjdk-12-jdk openjdk-12-jdk-headless openjdk-12-jre openjdk-12-jre-headless
 0 upgraded, 4 newly installed, 0 to remove and 212 not upgraded.
 Need to get 256 MB of archives.
 After this operation, 417 MB of additional disk space will be used.
 Do you want to continue? [Y/n] Y
 Get:1 http://ppa.launchpad.net/openjdk-r/ppa/ubuntu xenial/main amd64 openjdk-12-jre-headless amd64 12.0.1+12-4~16.04.1 [41.7 MB]
 Get:2 http://ppa.launchpad.net/openjdk-r/ppa/ubuntu xenial/main amd64 openjdk-12-jre amd64 12.0.1+12-4~16.04.1 [34.3 kB]
 Get:3 http://ppa.launchpad.net/openjdk-r/ppa/ubuntu xenial/main amd64 openjdk-12-jdk-headless amd64 12.0.1+12-4~16.04.1 [215 MB]
 Get:4 http://ppa.launchpad.net/openjdk-r/ppa/ubuntu xenial/main amd64 openjdk-12-jdk amd64 12.0.1+12-4~16.04.1 [10.9 kB]
 Fetched 256 MB in 27s (9,223 kB/s)
 Selecting previously unselected package openjdk-12-jre-headless:amd64.
 (Reading database … 64423 files and directories currently installed.)
 Preparing to unpack …/openjdk-12-jre-headless_12.0.1+12-4~16.04.1_amd64.deb …
 Unpacking openjdk-12-jre-headless:amd64 (12.0.1+12-4~16.04.1) …
 Selecting previously unselected package openjdk-12-jre:amd64.
 Preparing to unpack …/openjdk-12-jre_12.0.1+12-4~16.04.1_amd64.deb …
 Unpacking openjdk-12-jre:amd64 (12.0.1+12-4~16.04.1) …
 Selecting previously unselected package openjdk-12-jdk-headless:amd64.
 Preparing to unpack …/openjdk-12-jdk-headless_12.0.1+12-4~16.04.1_amd64.deb …
 Unpacking openjdk-12-jdk-headless:amd64 (12.0.1+12-4~16.04.1) …
 Selecting previously unselected package openjdk-12-jdk:amd64.
 Preparing to unpack …/openjdk-12-jdk_12.0.1+12-4~16.04.1_amd64.deb …
 Unpacking openjdk-12-jdk:amd64 (12.0.1+12-4~16.04.1) …
 Processing triggers for hicolor-icon-theme (0.15-0ubuntu1.1) …
 Processing triggers for mime-support (3.59ubuntu1) …
 Setting up openjdk-12-jre-headless:amd64 (12.0.1+12-4~16.04.1) …
 Setting up openjdk-12-jre:amd64 (12.0.1+12-4~16.04.1) …
 Setting up openjdk-12-jdk-headless:amd64 (12.0.1+12-4~16.04.1) …
 Setting up openjdk-12-jdk:amd64 (12.0.1+12-4~16.04.1) …

Using java -version you can check the installed java version

$ java -version
 openjdk version "12.0.1" 2019-04-16
 OpenJDK Runtime Environment (build 12.0.1+12-Ubuntu-416.04.1)
 OpenJDK 64-Bit Server VM (build 12.0.1+12-Ubuntu-416.04.1, mixed mode)

Install Oracle JDK

In order to install Oracle JDK, you need to add ppa:linuxuprising/java APT repository.

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

Now after running apt update

$ sudo apt update

You can go and find oracle-java12-installer using apt search command

$ sudo apt search oracle-java12-installer
 Sorting… Done
 Full Text Search… Done
 oracle-java12-installer/xenial,now 12.0.1-1~linuxuprising1 amd64 [installed]
   Oracle Java(TM) Development Kit (JDK) 12

And using apt install you can install it

$ sudo apt install oracle-java12-installer
 Reading package lists… Done
 Building dependency tree
 Reading state information… Done
 The following additional packages will be installed:
   oracle-java12-set-default
 Suggested packages:
   binfmt-support visualvm ttf-baekmuk | ttf-unfonts | ttf-unfonts-core ttf-kochi-gothic | ttf-sazanami-gothic ttf-kochi-mincho | ttf-sazanami-mincho ttf-arphic-uming
   firefox | firefox-2 | iceweasel | mozilla-firefox | iceape-browser | mozilla-browser | epiphany-gecko | epiphany-webkit | epiphany-browser | galeon | midbrowser
   | moblin-web-browser | xulrunner | xulrunner-1.9 | konqueror | chromium-browser | midori | google-chrome
 The following NEW packages will be installed:
   oracle-java12-installer oracle-java12-set-default
 0 upgraded, 2 newly installed, 0 to remove and 212 not upgraded.
 Need to get 0 B/36.5 kB of archives.
 After this operation, 138 kB of additional disk space will be used.
 Do you want to continue? [Y/n]

After you type Y character to continue installation, you will see two screens in regards to Oracle License Agreement policy. In order to continue and install Oracle JDK make sure you read that and if you agree click on Agree choice on the second screen

oracle-license-agreement-01

Image 1 of 2

Using java -version, you will see that Oracle JDK has been installed with this name: Java HotSpot(TM) 64-Bit Server VM. If you already installed openjdk 12 or other versions of java, this installer will override the Java path and JAVA_HOME variable to its location.

$ java -version
 java version "12.0.1" 2019-04-16
 Java(TM) SE Runtime Environment (build 12.0.1+12)
 Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing) 

Use jEnv to Switch JDKs

You can refer to Switch JDKs using jEnv page, if you have multiple JDKs installed on your machine, and you need to switch between them fast and without going and set $JAVE_HOME and $PATH environment variables

Leave a Reply