Installing Oracle Java 8 in Ubuntu

Image Courtesy: Pexels

Step 1: Add Oracle’s PPA(Personal Package Archive) to your list of sources so that Ubuntu knows where to check for the updates. Use add-apt-repository command for that.

sudo add-apt-repository ppa:webupd8team/java

Step 2: Then update your package repository by issuing the following command. apt-get is a command-line tool for handling packages.

sudo apt-get update

Steps 3: Then install Oracle JDK 8 using the following.

sudo apt-get install oracle-java8-installer

Step 4: Once it is installed successfully, check for the Java version.

javac -version

Step 5: If you have multiple Java packages installed on your machine, to check which version to use as the default type use the following command.

sudo update-alternatives --config java
Sample output of the above command

With the “auto mode”, highest priority alternative will always be selected as the default alternative even when a new or an upgraded package is installed whereas with the “manual mode”, regardless of the priority, your choice will always be considered as the default alternative until you choose another package.

Set JAVA_HOME environment variable

  1. First, find out where Java is installed. You can use the same command as the one we use in step 5 for that.

2. Next open /etc/environment file in any of your favorite editor

sudo nano /etc/environment

3. Add the following line at the end of the file and save it. (Replace the value for JAVA_HOME with the path of your java installation directory)

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

4. Finally reload it and check whether the environment variable has been properly set.

source /etc/environment
echo $JAVA_HOME