Linux Tutorial

How to Install Java on Ubuntu 19.04 [Complete Guide]

install-java-on-ubuntu-guide

Some programs/tools/utility on Ubuntu required java/JVM, without java these programs are not working. Are you facing same problem? Don’t worry!

Today I am going to cover in this article how to install Java on Ubuntu. This article will cover the complete tutorial step by step.

You can get java on Ubuntu via three packages JRE, OpenJDK and Oracle JDK.

Java and Java’s Virtual Machine (JVM) are widely used and required to run much software.

Check if java is already installed on Ubuntu

It is necessary to check if java is already installed, so run following command and check it.

If the java is not installed on your system you will get following result, and if java installed you will get some different output.

[email protected]:~$java --version

Command 'java' not found, but can be installed with:

sudo apt install openjdk-11-jre-headless  # version 11.0.3+7-1ubuntu1, or
sudo apt install default-jre              # version 2:1.11-71
sudo apt install openjdk-12-jre-headless  # version 12.0.1+12-1
sudo apt install openjdk-13-jre-headless  # version 13~13-0ubunt1
sudo apt install openjdk-8-jre-headless   # version 8u212-b01-1
[email protected]:~$

Prerequisites to install java on Ubuntu

What is the requirements to install java on Ubuntu, Of course you should know otherwise you will get the error or face the problems.

If you know the prerequisites, you can install any software without any problem. Following are prerequisites for installing java.

  • You have installed ubuntu (It should not be live boot).
  • Login user must be Sudo/root user
  • Active internet connection on System.

Install Default JRE/JDK

apt-get is a utility used to install, update, remove packages on Ubuntu. This is the easiest way to install java on Ubuntu, open terminal run command, sit back on the chair, have a coffee, and java or other package will be install automatic.

Before go to install java on Ubuntu you must update the packages list used by apt-get utility.

To update package list use following command

$sudo apt-get update

[email protected]:~$sudo apt-get update 
Hit:1 http://security.ubuntu.com/ubuntu disco-security InRelease               
Hit:2 http://in.archive.ubuntu.com/ubuntu disco InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu disco-updates InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu disco-backports InRelease
Reading package lists... Done
[email protected]:~$

Next step to install default-jre after completing package list updation.

$sudo apt-get install default-jre

[email protected]:~$sudo apt-get install default-jre
[email protected]:~$java --version
openjdk 11.0.3 2019-04-16
OpenJDK Runtime Environment (build 11.0.3+7-Ubuntu-1ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.3+7-Ubuntu-1ubuntu1, mixed mode, sharing)
[email protected]:~$

Next step install default JDK or OpenJDK on the on the system.

The JDK is usually only needed if you are going to compile Java programs or if the software that will use Java specifically requires it.

The JDK does contain the JRE, so there are no disadvantages if you install the JDK instead of the JRE, except for the larger file size.

Go through the following command to install JDK

$sudo apt-get install default-jdk

[email protected]:~$sudo apt-get install default-jdk
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  default-jdk-headless libice-dev libpthread-stubs0-dev libsm-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-11-jdk
  openjdk-11-jdk-headless x11proto-core-dev x11proto-dev xorg-sgml-doctools xtrans-dev
Suggested packages:
  libice-doc libsm-doc libx11-doc libxcb-doc libxt-doc openjdk-11-demo openjdk-11-source visualvm
The following NEW packages will be installed:
  default-jdk default-jdk-headless libice-dev libpthread-stubs0-dev libsm-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxt-dev
  openjdk-11-jdk openjdk-11-jdk-headless x11proto-core-dev x11proto-dev xorg-sgml-doctools xtrans-dev
0 upgraded, 16 newly installed, 0 to remove and 8 not upgraded.
Need to get 210 MB of archives.
After this operation, 226 MB of additional disk space will be used.
Do you want to continue? [Y/n] 

Install the Oracle JDK on Ubuntu

To install Oracle JDK java version 11 and 12, you must add new repository by using following command

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

[email protected]:~[email protected]:~$ sudo add-apt-repository ppa:linuxuprising/java

When you make entry of the repository server in repository list, you must update again repository list.

[email protected]:~$ sudo apt-get update 
Hit:1 http://ppa.launchpad.net/linuxuprising/java/ubuntu disco InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu disco InRelease                                      
Get:3 http://security.ubuntu.com/ubuntu disco-security InRelease [97.5 kB]                     
Get:4 http://in.archive.ubuntu.com/ubuntu disco-updates InRelease [97.5 kB]                                
Hit:5 http://ppa.launchpad.net/webupd8team/java/ubuntu disco InRelease                                     
Get:6 http://in.archive.ubuntu.com/ubuntu disco-backports InRelease [88.8 kB]              
Fetched 284 kB in 3s (112 kB/s)     
Reading package lists... Done
[email protected]:~$

Now use following command to install oracle JDK 12 on Ubuntu 19.04 Server, using following command

$sudo apt install oracle-java12-installer

[email protected]:~$sudo apt install oracle-java12-installer
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  gsfonts-x11 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
The following NEW packages will be installed:
  gsfonts-x11 oracle-java12-installer oracle-java12-set-default
0 upgraded, 3 newly installed, 0 to remove and 8 not upgraded.
Need to get 43.6 kB of archives.
After this operation, 236 kB of additional disk space will be used.
Do you want to continue? [Y/n] 
[email protected]:~$
[email protected]:~$javac -version 
javac 12.0.1
[email protected]:~$

Managing java on Ubuntu

You have installed java on your system, later newer version of java will launched. Now you think to install new version. No, problem at all, you can install new version.

But if you want your system use newer java version, you will have to configure. Following command is useful

$sudo update-alternatives –config java

You will see all installed package on your system, select number and hit enter.

[email protected]:~$sudo update-alternatives --config java
[sudo] password for vijay: 
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-12-oracle/bin/java          1091      manual mode

Press  to keep the current choice[*], or type selection number: 
[email protected]:~$

Uninstall Oracle Java12

To remove oracle java12, run command to remove the installer script. You can use apt-get remove to uninstall program from the Ubuntu 19.04 desktop/server.

[email protected]:~$sudo apt-get remove oracle-java12-installer
[email protected]:~$

Conclusion

By now you have learnt how to install java on ubuntu. Now you can install default jre and jdk, and oracle java. Also you can install multiple version of java.

So If you have any question, write in commentbox now. you can write me [email protected]

Cheers!

Skip to toolbar