Skip to content

Instantly share code, notes, and snippets.

@starlinq
Last active January 27, 2024 19:11
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save starlinq/9ee5209ceb32b7e05817e714fe530be3 to your computer and use it in GitHub Desktop.
Save starlinq/9ee5209ceb32b7e05817e714fe530be3 to your computer and use it in GitHub Desktop.
How to install Java JDK8 in Ubuntu 16.04

How to install Java JDK8 in Ubuntu 16.04

Let's consider a JDK8 installation using 2 different versions: default and Oracle. The default version is easiest because it is packaged with Ubuntu.

There is default Java installation called the JDK (Java Development Kit). The JDK is usually only needed if you are going to to do some software developmwent using Java or if some software requires it. The JDK does contain the JRE.

Installing the default JDK

This will install OpenJDK 8, the latest and recommended version.

First, update the package index:

sudo apt update

You can install the JDK with the following command:

sudo apt-get install default-jdk

Installing the Oracle JDK

  1. Download the latest JDK file

A current link is http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html, if there is no such page you can find it from general download page http://www.oracle.com/technetwork/java/javase/downloads/index.html

The latest version is Java SE Development Kit 8u181.

The archive file for Linux x64 is jdk-8u181-linux-x64.tar.gz

Before the file can be downloaded, you must accept the license agreement. The archive binary can be installed by anyone (not only root users), in any location that you can write to. However, only the root user can install the JDK into the system location.

If you download it from web browser, in typical configuration the file will be saved to ~/Downloads directory

  1. Open a terminal using Ctrl+Alt+T key combination

  2. Change a current directory to ~/Downloads

$ cd ~/Downloads
  1. Unpack the tarball
$ tar zxvf jdk-8u181-linux-x64.tar.gz

The Java Development Kit files are installed in a directory called jdk1.8.0_version in the current directory.

You will find a folder with the name as jdk1.8.0_181.

  1. Let's move the directory to default system directory
sudo mv ./jdk1.8.0_181 /usr/lib/jvm

The complete folder “jdk1.8.0_version” will be moved to /usr/lib/jvm.

Delete the .tar.gz file if you want to save disk space.

  1. Inform Ubuntu to use this JDK
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0_181/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0_181/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.8.0_181/bin/javaws" 1

This will assign Oracle JDK a priority of 1, which means that installing other JDKs may replace it as the default. Use a higher priority if you want Oracle JDK to remain the default.

The executables java, javac, javaws are probably the most frequently used.

  1. If you have multiple java installations you need to configure it by
sudo update-alternatives --config java

in my case (before this installation, I have installed OpenJDK8), it gives

There are 2 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-8-openjdk-amd64/jre/bin/java   1081      manual mode
  2            /usr/lib/jvm/jdk1.8.0_181/bin/java               1         manual mode

Press <enter> to keep the current choice[*], or type selection number: 

In this case, I select the last number and press enter to exit this utility i.e. in this example it is the number 2.

If you are going to use javac and javaws, repeat the above for:

sudo update-alternatives --config javac
sudo update-alternatives --config javaws
  1. Set environment variables

Edit the environment file:

sudo gedit /etc/environment

My current environment file contains:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

Update the existing PATH variable by adding the below bin folders, separated with a colon :.

/usr/lib/jvm/jdk1.8.0_181/bin:/usr/lib/jvm/jdk1.8.0_181/db/bin:/usr/lib/jvm/jdk1.8.0_181/jre/bin

Paths can be different based on version and update, here the version is 1.8 and the update is 181. Add the below variables at the end of environment file, making changes for your specific version and update. I commented out original line.

# PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_181/bin:/usr/lib/jvm/jdk1.8.0_181/db/bin:/usr/lib/jvm/jdk1.8.0_181/jre/bin"
J2SDKDIR="/usr/lib/jvm/jdk1.8.0_181"
J2REDIR="/usr/lib/jvm/jdk1.8.0_181/jre*"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_181"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_181/db"

Save changes and close the file. Log out and log in again.

You can now test whether the environment variable has been set by executing the following command:

echo $JAVA_HOME

Verify the Java version

java -version

The output should resemble the following:

java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

You should be able to see your installed java version which means you have successfully installed the Oracle JDK.

@Tridip-Barua
Copy link

Finally it works. Thank you!!!

@jardeleko
Copy link

good job, Its a fine docs man

@StefanoWen
Copy link

perfectly worked on ubuntu 16.04!nice job!

@jhutanda
Copy link

jhutanda commented Jan 4, 2021

Thank you. This article is very useful. Finally, I have done my installation.

@gisegalaburri
Copy link

hey man! You're the best!

@VictorAviles
Copy link

Thank you

@ShedrackGodson
Copy link

Worked like a charm!!

@himanshu-anonymous
Copy link

umm Somebody Please Help me

whenever i am trying to execute this command "java -version"
i am getting this error "zsh: exec format error: java"

what should i do??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment