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.

[ac-button size=”large” color=”green” style=”flat” icon=”fa-link” url=”https://www.2daygeek.com/category/java/” target=”_blank”]List of method to install JAVA on Linux[/ac-button]

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.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

28 Comments on “Install JAVA (Oracle JAVA & openJDK) via JAVA Alternatives Method”

  1. This is an amazing guide. It really did help a lot. Thanks so much!
    I was stuck at step 4, but then I used the following command to uncompress my tar files in /opt/ (of course I googled it!):
    sudo tar -zxvf jdk-8u20-linux-x64.tar.gz -C /opt/

  2. Excelent work!

    But I have a dude:

    the JAVA_HOME and JRE_HOME variables, are there paths to a directory?
    export JAVA_HOME=/opt/jdk1.8.0_20/bin
    export JRE_HOME=/opt/jdk1.8.0_20/bin

    or are there paths to a file?
    export JAVA_HOME=/opt/jdk1.8.0_20/bin/java
    export JRE_HOME=/opt/jdk1.8.0_20/bin/jar

    regards

  3. @tiomte ,

    For manual software installing, i advise you to download and extract the software package to /opt or /usr/local/src
    directory.

    In step-4 first part is java global location so you need to put the same like below.
    sudo update-alternatives –install /usr/bin/java java (second part – you need mention your new java directory where you extracted the java on your system)

  4. It takes time for me because i downloaded it at /home/lordlaw
    I’ve been stuck at step 4 or the installing process -_- HAHA but it’s okay I googled it and copy the files to /opt but it’s not that easy because i need to log in in terminal as the root 🙂 and that’s why I’m very thankful to google!!! ^_^
    And ofcourse for this Guide Thank you so much!! muwah!!! love you! xD
    I’m just very happy! thank you for the easy guideliness!

    God Bless you! :))

  5. @DC Mishra,

    I have slightly modified the command and its start downloading now, pls use below command and let me know.

    # 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/8u20-b26/jdk-8u20-linux-i586.tar.gz”

    1. Your modified command is not working too-
      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/8u20-b26/jdk-8u20-linux-i586.tar.gz”–2014-08-28 21:26:58– http://xn--no-cookies-nu6e/
      Resolving –no-cookies (xn--no-cookies-nu6e)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--no-cookies-nu6e’
      –2014-08-28 21:27:03– http://xn--no-cookies-nu6e/
      Resolving –no-cookies (xn--no-cookies-nu6e)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--no-cookies-nu6e’
      –2014-08-28 21:27:03– http://xn--no-check-certificate-2t2l/
      Resolving –no-check-certificate (xn--no-check-certificate-2t2l)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--no-check-certificate-2t2l’
      –2014-08-28 21:27:03– http://xn--no-check-certificate-2t2l/
      Resolving –no-check-certificate (xn--no-check-certificate-2t2l)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--no-check-certificate-2t2l’
      –2014-08-28 21:27:03– http://xn--header-vg0c/
      Resolving –header (xn--header-vg0c)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--header-vg0c’
      –2014-08-28 21:27:03– http://xn--header-vg0c/
      Resolving –header (xn--header-vg0c)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--header-vg0c’
      –2014-08-28 21:27:03– ftp://xn--cookie-oi0c/
      => ‘.listing’
      Resolving “cookie (xn--cookie-oi0c)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--cookie-oi0c’
      –2014-08-28 21:27:04– ftp://xn--cookie-oi0c/
      => ‘.listing’
      Resolving “cookie (xn--cookie-oi0c)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--cookie-oi0c’
      –2014-08-28 21:27:04– http://[gpw_e24=http://www.oracle.com/%5D/
      Resolving gpw_e24=http://www.oracle.com/ (gpw_e24=http://www.oracle.com/)… failed: Name or service not known.
      wget: unable to resolve host address ‘gpw_e24=http://www.oracle.com/’
      bash: “http://download.oracle.com/otn-pub/java/jdk/8u20-b26/jdk-8u20-linux-i586.tar.gz”: No such file or directory
      Please, look into again.

      1. It’s working for me. Then better visit oracle website and download the 32-bit package and follow the same steps to do that

  6. @D C Mishra,

    Use the below command to download the 32-bit package and Follow the same steps.

    # 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/8u20-b26/jdk-8u20-linux-i586.tar.gz”

    If you face any trouble let me know to check further.

    1. I got the following after your suggested command

      -2014-08-27 21:07:54– http://xn--no-cookies-nu6e/
      Resolving –no-cookies (xn--no-cookies-nu6e)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--no-cookies-nu6e’
      –2014-08-27 21:07:54– http://xn--no-cookies-nu6e/
      Resolving –no-cookies (xn--no-cookies-nu6e)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--no-cookies-nu6e’
      –2014-08-27 21:07:54– http://xn--no-check-certificate-2t2l/
      Resolving –no-check-certificate (xn--no-check-certificate-2t2l)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--no-check-certificate-2t2l’
      –2014-08-27 21:07:54– http://xn--no-check-certificate-2t2l/
      Resolving –no-check-certificate (xn--no-check-certificate-2t2l)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--no-check-certificate-2t2l’
      –2014-08-27 21:07:54– http://xn--header-vg0c/
      Resolving –header (xn--header-vg0c)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--header-vg0c’
      –2014-08-27 21:07:54– http://xn--header-vg0c/
      Resolving –header (xn--header-vg0c)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--header-vg0c’
      –2014-08-27 21:07:55– ftp://xn--cookie-oi0c/
      => ‘.listing’
      Resolving “cookie (xn--cookie-oi0c)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--cookie-oi0c’
      –2014-08-27 21:07:55– ftp://xn--cookie-oi0c/
      => ‘.listing’
      Resolving “cookie (xn--cookie-oi0c)… failed: Name or service not known.
      wget: unable to resolve host address ‘xn--cookie-oi0c’
      –2014-08-27 21:07:55– http://[gpw_e24=http://www.oracle.com/%5D/
      Resolving gpw_e24=http://www.oracle.com/ (gpw_e24=http://www.oracle.com/)… failed: Name or service not known.
      wget: unable to resolve host address ‘gpw_e24=http://www.oracle.com/’
      bash: “http://download.oracle.com/otn-pub/java/jdk/8u20-b26/jdk-8u20-linux-i586.tar.gz”: No such file or directory

      Please look into this

  7. Thank you! I am a Noob. Win? No prob, Linux? Noob. I almost didn’t catch that you chdir to . . / opt $
    I was d/l’ing away at about 85% complete when I noticed it. Haha, I thought OMG! 😉
    That was perfect. Worked perfect, even for me. Thank you so much. I owe you!!

Leave a Reply