Tuesday, February 6, 2018

How to Install Java on Ubuntu

In this post we’ll see how to install Java on Ubuntu. You can install either OpenJDK or Oracle JDK here steps to install both are given.

Note that steps given here are to install Java 8 on Ubuntu 16.04.

Installing OpenJDK on Ubuntu

Installing OpenJDK on Ubuntu is very easy you just need to run the following command.

sudo apt-get install openjdk-8-jdk

This command will both download and install the required packages.

If you want just the Java run time environment (JRE) then you can use the following command.

sudo apt-get install openjdk-8-jre

Installing Oracle JDK on Ubuntu

Script for installing Oracle JDK using apt-get is maintained by third party, you can use that to install Oracle JDK or download latest JDK from Oracle’s site and install it yourself. Here both of the ways to install Java are given.

Using apt-get

For installing using apt-get you need to add Oracle’s PPA (Personal Package Archive) then update the package repository using that information.

Use the following command to add PPA

sudo add-apt-repository ppa:webupd8team/java

Use the following command to update packages.

sudo apt-get update

Then for installing Java 8 use the following command.

sudo apt-get install oracle-java8-installer

Manually downloading and installing Oracle JDK

You can download the required JDK version from here - http://www.oracle.com/technetwork/java/javase/downloads/index.html

Then create a directory where you want to unpack the downloaded tar ball.

sudo mkdir /usr/local/java

Change directory to Downloads where tar ball will be downloaded by default and copy it to the created directory.

sudo cp -r jdk-8u151-linux-x64.tar.gz /usr/local/java

Then unpack the tar in the directory /usr/local/java using the following command.

sudo tar zxvf jdk-8u151-linux-x64.tar.gz

Setting JAVA_HOME environment variable and path

You need to set the JAVA_HOME environment variable so that the applications know the Java installation location.

Open the /etc/environment file using gedit.

sudo gedit /etc/environment

Add JAVA_HOME environment variable at the end of the file.

JAVA_HOME="/usr/local/java/jdk1.8.0_151"

Also append the path to /bin directory of your Java installation to the existing PATH variable in /etc/environment file.

:/usr/local/java/jdk1.8.0_151/bin 

Save and close the file. You need to reload it so that the changes are visible.

source /etc/environment

If you echo JAVA_HOME now you should get the path you set, try that using the following command.

echo $JAVA_HOME

That's all for this topic How to Install Java on Ubuntu. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. Installing Ubuntu Along With Windows
  2. Installing Hadoop on a Single Node Cluster in Pseudo-Distributed Mode
  3. How to Create Ubuntu Bootable USB
  4. Primitive Data Types in Java
  5. Java for Loop With Examples

You may also like-

  1. How to Pass Command Line Arguments in Eclipse
  2. Creating a Maven Project in Eclipse
  3. JVM Run-Time Data Areas - Java Memory Allocation
  4. Heap Memory Allocation in Java
  5. Just In Time Compiler (JIT) in Java
  6. SerialVersionUID And Versioning in Java Serialization
  7. How HashMap Internally Works in Java
  8. Introduction to Hadoop Framework