Install JAVA (Oracle JAVA & openJDK) via JAVA Alternatives Method

JAVA is necessary to run certain applications in Linux and windows environment. I already installed OpenJDK 7 in my system and I’m going to install oracle java 8 using java alternatives method which is very simple and handy. Just dig our article.

List of method to install JAVA on Linux

1) Check current java version

Use the below command to check installed java version on your system.

$ java -version
java version "1.7.0_91"
OpenJDK Runtime Environment (IcedTea 2.6.3) (7u91-2.6.3-0ubuntu0.15.10.1)
OpenJDK 64-Bit Server VM (build 24.91-b01, mixed mode)

The output clearly shows that, i have already installed OpenJDK 7 in my system.

2) Download oracle java archive file

Use the below command to download the oracle java 8 archive file. Make sure oracle uses cookies, so use according to that.

[Downloading Oracle Java 8 archive file]
$ sudo 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/8u66-b17/jdk-8u66-linux-x64.tar.gz"

3) Extract the archive file

Use the below tar command to extract the tar.gz archive file.

[Extract Oracle Java Archive file]
$ sudo tar -zxvf jdk-8u66-linux-x64.tar.gz

4) Install oracle java 8 using Alternatives Method

Use the below command to Install oracle java 8 on your system using JAVA alternatives method. Make sure you need to mention your java path (Newly downloaded)

[Install Oracle Java using Java Alternatives Method]
$ sudo update-alternatives --install /usr/bin/java java /opt/jdk1.8.0_66/bin/java 2

5) Choose alternative java version

Use the below command to list installed java version on your system. Here i’m going to choose oracle java 8 which is installed newely on my system. just enter the number which is showing under Selection area to switch to alternative version of java.

[Setup Newly installed java version as a default]
$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      auto mode
  1            /opt/jdk1.8.0_66/bin/java                        2         manual mode
  2            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      manual mode

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /opt/jdk1.8.0_66/bin/java to provide /usr/bin/java (java) in manual mode

You can setup javac and jar also using alternative method. If you are not setting up these, it will use older one which is auto mode.

[Setup javac]
$ sudo update-alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_66/bin/javac 2

[See the below output for javac]
$ sudo update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-openjdk-amd64/bin/javac   1071      auto mode
  1            /opt/jdk1.8.0_66/bin/javac                    2         manual mode
  2            /usr/lib/jvm/java-7-openjdk-amd64/bin/javac   1071      manual mode

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /opt/jdk1.8.0_66/bin/javac to provide /usr/bin/javac (javac) in manual mode

Setup jar using alternative method.

[Setup jar]
$ sudo update-alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_20/bin/jar 2

[See the below output for jar]
$ sudo update-alternatives --config jar
There are 2 choices for the alternative jar (providing /usr/bin/jar).

  Selection    Path                                       Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-openjdk-amd64/bin/jar   1071      auto mode
  1            /opt/jdk1.8.0_66/bin/jar                    2         manual mode
  2            /usr/lib/jvm/java-7-openjdk-amd64/bin/jar   1071      manual mode

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /opt/jdk1.8.0_66/bin/jar to provide /usr/bin/jar (jar) in manual mode

6) once again check java version

Use the below command to check your new installed java version.

$ java -version
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

Yes, we have successfully installed Oracle java 8 and showing output also above But environemnt variables still using older one, see the below output.

$ echo $JAVA_HOME
/usr/lib/jvm/java-7-openjdk-amd64/bin/java

$ echo $JRE_HOME
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

7) Setup JAVA Environment Variables

You should Setup Environment Variables for java because all of java based application’s use environment variables to work. Open your .bashrc file and add the below lines to end of the file. Make sure you need to mention your path instead of us. Save and exit.

$ sudo nano .bashrc
JAVA_HOME="/opt/jdk1.8.0_66/bin/java"
JRE_HOME="/opt/jdk1.8.0_66/bin/jar"
PATH=$PATH:$HOME/bin:JAVA_HOME:JRE_HOME

After saving .bashrc file, run the below command to make it work

$ sudo source ~/.bashrc

Now, you have successfully did setup the Environment Variables, Use the below command to check whether its setup is done properly or not.

$ echo $JAVA_HOME
/opt/jdk1.8.0_66/bin/java

$ echo $JRE_HOME
/opt/jdk1.8.0_66/bin/jar

Now its showing new Environment Variables. Please stay tune with us…Good Luck.

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...

Shares
Close
Please support the site
By clicking any of these buttons you help our site to get better