How to install Java JDK on Ubuntu

This tutorial shows you how to install OpenJDK 8,11 and Oracle JDK 8,11 on Ubuntu 18.04

1. Install OpenJDK (APT Install)

Install OpenJDK via apt install command automatically.

1.1 Search all available openJDK in the default repository.


$ sudo apt search openjdk

...
openjdk-8-jdk/bionic-updates,bionic-security,now 8u181-b13-1ubuntu0.18.04.1 amd64 [installed]
  OpenJDK Development Kit (JDK)

openjdk-11-jdk/bionic-updates,bionic-security,now 10.0.2+13-1ubuntu0.18.04.3 amd64 [installed]
  OpenJDK Development Kit (JDK)
...

1.2 Install openjdk-8-jdk or openjdk-11-jdk


$ sudo apt install openjdk-8-jdk

$ sudo apt install openjdk-11-jdk

1.3 Verification. If openjdk-8-jdk is installed.


$ java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-1ubuntu0.18.04.1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

2. Install OpenJDK 11 Manually

This method should work in any JDK version, including Oracle JDK, just download the JDK, extracts it and install it manually via update-alternatives --install

2.1 Download OpenJDK 11, and extracts it.


$ wget -c  https://download.java.net/java/GA/jdk11/13/GPL/openjdk-11.0.1_linux-x64_bin.tar.gz
$ sudo mkdir /opt/java
$ sudo tar -zxf openjdk-11.0.1_linux-x64_bin.tar.gz -C /opt/java

$ ls -lsa /opt/java
total 12
4 drwxr-xr-x 3 root root 4096 Nov   8 15:48 .
4 drwxr-xr-x 5 root root 4096 Nov   8 15:46 ..
4 drwxr-xr-x 8 root root 4096 Nov   8 15:48 jdk-11.0.1

2.2 Install it.


$ sudo update-alternatives --install /usr/bin/java java /opt/java/jdk-11.0.1/bin/java 100

2.3 If multiple JDK are installed, select the right one.


$ sudo update-alternatives --config java

There are 4 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101      auto mode
* 1            /opt/java/jdk-11.0.1/bin/java                    100       manual mode
  2            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101      manual mode
  3            /usr/lib/jvm/java-11-oracle/bin/java             1091      manual mode
  4            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

2.4 Verification.


$ java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)

3. Install Oracle JDK

The Oracle JDK is not available in the default repository, we need to add a 3rd party repository in order to download it.

Note
Oracle uses a new license on the Oracle JDK 11. It’s free to download for development and testing only. If it’s used in production, you need to pay.

3.1 Install Oracle JDK 11


$ sudo add-apt-repository ppa:linuxuprising/java 
$ sudo apt update
$ sudo apt install oracle-java11-installer

3.2 Install Oracle JDK 8


sudo add-apt-repository ppa:webupd8team/java
sudo apt update
sudo apt install oracle-java8-installer

4. Multiple JDK Installed

Uses this sudo update-alternatives --config java to define which JDK to use.


$ sudo update-alternatives --config java

There are 4 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101      auto mode
* 1            /opt/java/jdk-11.0.1/bin/java                    100       manual mode
  2            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1101      manual mode
  3            /usr/lib/jvm/java-11-oracle/bin/java             1091      manual mode
  4            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

5. Add JAVA_HOME

Always add JAVA_HOME into the system variable, so that other applications are able to find the location of the Java installation.


$ sudo vim /etc/profile.d/java.sh
/etc/profile.d/java.sh

export JAVA_HOME=/opt/java/jdk-11.0.1
export PATH=$PATH:$JAVA_HOME/bin
/etc/profile.d/*.sh
Files with the .sh extension in the /etc/profile.d directory get executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh)

Done.

References

  1. Install Oracle JDK 11
  2. Oracle JDK Releases for Java 11 and Later
  3. Ubuntu – EnvironmentVariables
  4. How to Install Oracle JDK 8 On Debian

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
56 Comment threads
42 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
57 Comment authors
mkyongbhaveshYashaswisachin Duablackbird014 Recent comment authors
newest oldest most voted
bhavesh
Guest
bhavesh

its really a helpful post

Yashaswi
Guest
Yashaswi

for those who are looking to install SUN/Oracle jdk chk this –>> http://www.wikihow.com/Install-Oracle-Java-JDK-on-Ubuntu-Linux

sachin Dua
Guest
sachin Dua

Sir
sachin@sachin-Lenovo-G550:~$ java -version
java version “1.7.0_21”
OpenJDK Runtime Environment (IcedTea 2.3.9) (7u21-2.3.9-1ubuntu1)
OpenJDK Server VM (build 23.7-b01, mixed mode)

when I apply this command this showe that java is installed in my system,but when I am going to compile any java program it gives the following error

sachin@sachin-Lenovo-G550:~/programs$ javac abc.java
The program ‘javac’ can be found in the following packages:
* default-jdk
* ecj
* gcj-4.6-jdk
* gcj-4.7-jdk
* openjdk-7-jdk
* openjdk-6-jdk
Try: sudo apt-get install

please tell me how to get rid of it & run my java program

mkyong
Guest
mkyong

Weird… Ubuntu should copy the Java/bin folder to your system path automatically. Nvm, just put the JAVA/bin folder to your environment.

export PATH=$PATH:$JAVA_HOME/bin

blackbird014
Guest
blackbird014

always interesting. Thanx for posting
a little remark.
you can correct
opt-cache search openjdk
with
apt-cache search openjdk

I even copied and pasted half asleep haha

mkyong
Guest
mkyong

Thanks for the typo, article is updated.

Kazuko
Guest
Kazuko

I loved as much as you will receive carried out
right here. The sketch is attractive, your authored subject matter stylish.
nonetheless, you command get bought an impatience over that
you wish be delivering the following. unwell unquestionably come
more formerly again as exactly the same nearly a lot often inside
case you shield this increase.

Prakhar
Guest
Prakhar

You probably forgot to close tag after "JAVA_HOME" in your blog. It has messed up the formatting below line "in system path each time the terminal is started,"...

Prakhar
Guest
Prakhar

oops..! it picked up tag and screwed my comment's formatting also.

mkyong
Guest
mkyong

Thanks, fixed.

trackback
??Java | ???

[…] apt????(??) […]

trackback
Hadoop Cluster using Whirr – BYON in AWS VPC « blog@cloud-kinetics
kava
Guest
kava

Hi , While installing i am getting this error ..pls look at this give me suggestions..how to resolve.., Err http://in.archive.ubuntu.com/ubuntu/ precise-updates/universe openjdk-7-jre-lib all 7~u3-2.1.1~pre1-1ubuntu3 Something wicked happened resolving ‘in.archive.ubuntu.com:http’ (-5 – No address associated with hostname) Err http://in.archive.ubuntu.com/ubuntu/ precise/main ca-certificates-java all 20110912ubuntu6 Something wicked happened resolving ‘in.archive.ubuntu.com:http’ (-5 – No address associated with hostname) Err http://in.archive.ubuntu.com/ubuntu/ precise/main tzdata-java all 2012b-1 Something wicked happened resolving ‘in.archive.ubuntu.com:http’ (-5 – No address associated with hostname) Get:1 http://in.archive.ubuntu.com/ubuntu/ precise-updates/universe openjdk-7-jre-headless i386 7~u3-2.1.1~pre1-1ubuntu3 [30.2 MB] Get:2 http://in.archive.ubuntu.com/ubuntu/ precise/main libbonobo2-common all 2.32.1-0ubuntu1 [56.7 kB] Get:3 http://in.archive.ubuntu.com/ubuntu/ precise/main libbonobo2-0 i386 2.32.1-0ubuntu1 [222 kB] Get:4 http://in.archive.ubuntu.com/ubuntu/ precise/main libgnomevfs2-common… Read more »

sanjay
Guest
sanjay

hiiiiiiiiiii i won’t get correct result of how to open ecllipse in linux(ubuntu)

Weimin Ma
Guest
Weimin Ma

Hi, I am trying install sun-java6-jdk but I got the following errors. It used to work for me before. So it seems that it has been removed from partners. Does anyone else see this? Can someone tell me how to download it from oracle? sudo apt-get install sun-java6-jdk Reading package lists… Done Building dependency tree Reading state information… Done The following extra packages will be installed: gsfonts-x11 java-common odbcinst odbcinst1debian1 sun-java6-bin sun-java6-jre unixodbc Suggested packages: default-jre equivs sun-java6-demo default-jdk-doc sun-java6-source sun-java6-plugin ia32-sun-java6-plugin sun-java6-fonts ttf-kochi-gothic ttf-sazanami-gothic ttf-kochi-mincho ttf-sazanami-mincho ttf-arphic-uming libmyodbc odbc-postgresql tdsodbc unixodbc-bin The following NEW packages will be installed: gsfonts-x11… Read more »

B G Kavinga
Guest
B G Kavinga

First check your internet connection, if it’s working do as follows.
>sudo apt-get update
>sudo apt-get install sun-java6-jdk

or
check http://bgkavinga.blogspot.com/2011/09/start-up-minicom-sudo-minicom-s.html
for offline installation.

sanjay
Guest
sanjay

please check the root directory once

Gihan
Guest
Gihan

This will be use full if you have no internet access in your machine.

<a href="http://bgkavinga.blogspot.com/2011/09/start-up-minicom-sudo-minicom-s.html">Setup Java Development Kit(JDK) on Ubuntu(Offline)</a>
Bayou
Guest
Bayou

Thanks a lot for the instruction. I just downloaded some java plugins into my ubuntu. What folders to put them to in order to be compiled by javac to the correct path.

Dude
Guest
Dude

Sun/Oracle java is no longer in ubuntu or supported, install it from tarball from there website… or this PPA:

sudo add-apt-repository ppa:ferramroberto/java

sudo apt-get update

sudo apt-get install sun-java6-jdk

Russell Lee
Guest
Russell Lee

is that all u do?

Meee
Guest
Meee

Thank you GGG, working perfectly. :D

suho
Guest
suho

To install Oracle Java 7 and 6 on Ubuntu 12.04 this post will help
http://suhothayan.blogspot.com/2012/04/how-to-instal-java-in-ubuntu.html

Ranjan Singh
Guest
Ranjan Singh

I tried many times for installing java as same code as Windows….but not to do that..
But It is easy as according to your Instruction.

Santosh
Guest
Santosh

Thank u…

CV
Guest
CV

Thank you!

Adria Figuera
Guest
Adria Figuera

Hi from Catalonia! mkyong you save my java developments and when I want something for linux it seems your web is also the best… Congratulations for your web and keep on!!!

trackback
Client stuck at 98% when loading

[…] then update. Useful links: http://rsbuddy.com/forum/%5Bguide%5D-how-run-rsbuddy-linux-117146.html How to install java jdk on ubuntu (linux) Spoiler: iDungeon Pro :: Runescape Botting VPS Sythe.org – Runescape Cheating […]

bruno
Guest
bruno

thankzzzz!

trackback
Grails para novatos: Primeros pasos en la búsqueda del Santo Grails | hkadejo
Brandon
Guest
Brandon

This will work, but the proper way to do this is to use…

sudo apt-get install sun-java6-jdk sun-java6-jre

Rajesh
Guest
Rajesh

to install jdk use this
sudo apt-get install openjdk-6-jdk openjdk-6-jre
Looks like oracle just changed the package names to get rid of ‘sun’ from the name.

Mike
Guest
Mike

Open JDK is a different package entirely: http://openjdk.java.net/

Although it works for almost everything you’d want to do.

hooman raesi
Guest
hooman raesi

The apt-get install sun-java6-jdk sun-java6-jre doesn’t work for me , i use openjdk and now i have jdk on my system
thanks alot

Martin
Guest
Martin

export JAVA_HOME=”/usr/lib/jvm/java-6-sun-1.6.0.06;”

I guess you don’t need a semicolon here, do you?

manandvan
Guest
manandvan

thanks akumu for your tips. i dont know why java dont make self installing setup file like windows.

trackback
Install java jdk on ubuntu « MahastMahast

[…] source […]

Luiz Eduardo
Guest
Luiz Eduardo

i think you forgot to append this to bashrc

export PATH=$PATH:$JAVA_HOME/bin

vincent
Guest
vincent

thanks to kishore and his links….. i could install jdk without internet connection on my machine.

http://kishor15389.blogspot.com/2010/11/install-jdk-on-ubuntu-any-version.html

trackback
How to install the JDK on Ubuntu Linux - Java Programming Forums

[…] JAVA_HOME="/usr/lib/jvm/java-6-sun-1.6.0.06;" Ported from: How to install java jdk on ubuntu (linux) function fbs_click() […]

Kishor
Guest
Kishor

IF U ARE LOOKING TO INSTALL JDK WITHOUT INTERNET CONNECTION.!IT IS POSSIBLE

IF DOWNLOAD IT FROM OTHER SYSTEM WHICH HAVE INTERNET…

IT TAKE FEW STEPS TO INSTALL THAT..

FOR MORE DETAILED STEPS PLEASE VISIT FALLOWING LINK:

http://kishor15389.blogspot.com/2010/11/install-jdk-on-ubuntu-any-version.html

I’M SURE IT DEFINITELY WORKS…

manikandan
Guest
manikandan

what means “remastering sys “