Install Oracle Java

Debian, Ubuntu, etc.

Install Oracle Java 1.7

# add ppa repository
sudo add-apt-repository ppa:webupd8team/java
sudo apt update

# install java
sudo apt install oracle-java7-installer

# set as default
sudo apt install oracle-java7-set-default

Install Oracle Java 1.8

# add ppa repository
sudo add-apt-repository ppa:webupd8team/java
sudo apt update

# install java
sudo apt install oracle-java8-installer

# set as default
sudo apt install oracle-java8-set-default

CentOS, RedHat, etc.

Install Oracle Java 1.7

# install JRE
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.rpm"
sudo yum localinstall jre-7u79-linux-x64.rpm

# install JDK
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm"
sudo yum localinstall jdk-7u79-linux-x64.rpm

Install Oracle Java 1.8

# install JRE
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" \
"http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jre-8u60-linux-x64.rpm"

sudo yum localinstall jre-8u60-linux-x64.rpm
# install JDK
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.rpm"
sudo yum localinstall jdk-8u60-linux-x64.rpm

Set default java

a) Using alternatives

sudo alternatives --config java

Sample output:

There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
   2           /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java
   3           /usr/java/jdk1.8.0_60/jre/bin/java

Enter to keep the current selection[+], or type selection number: 

If the java version you want to use is not displayed then we need to add it to alternatives:

alternatives --install /usr/bin/java java /usr/java/jdk1.8.0_60/bin/java 22
alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.8.0_60/bin/javaws 22
alternatives --install /usr/bin/javac javac /usr/java/jdk1.8.0_60/bin/javac 22
alternatives --install /usr/bin/jar jar /usr/java/jdk1.8.0_60/bin/jar 22
alternatives --install /usr/bin/javadoc javadoc /usr/java/jdk1.8.0_60/bin/javadoc 22
alternatives --config java
alternatives --config javaws
alternatives --config javac
alternatives --config jarTODO

b) Using environment variables

Many applications use JAVA_HOME or JRE_HOME environment variables to determine the installed location.

If you want the JAVA_HOME environment variable to be set for all users add the following line to ‘/etc/environment’ file.
export JAVA_HOME=/usr/java/jdk1.8.0_60/jre

An easy way to append it to the files is to run this command:

sudo sh -c "echo export JAVA_HOME=/usr/java/jdk1.8.0_60/jre >> /etc/environment"

September 19th, 2017

Posted In: tutorials

Tags: , , , ,

Leave a Comment