Skip to content →

Java 8 JDK – Installation in EC2 linux instance

Last Saturday, I’ve made a short introduction to Java 8. I’ve exposed some of the novelties of the new release and also answered an important question: can I move to Java 8 without breaking old projects?

Today I will go a step further and guide you in the installation of Oracle’s Java 8 JDK on Linux. The JDK is the development kit for Java software development and also a requirement for running some applications, such as Glassfish [1].

I will cover only the installation via command line, as I found myself in trouble while downloading Java on Linux without a GUI. This is a common problem for those installing on Linux server versions and for those who use automated installation scripts. This procedure can be used in the EC2 Linux versions of Amazon Web Services and can be applied to other Java versions.

So, what is the problem with the Java download via command line?

On Oracle’s installation guide [2], it is written something that I’ve found out myself before:

Before the file can be downloaded, you must accept the license agreement.

To accept the licence agreement, you must tick on the website the radio box stating that you accept it (see image).

Oracle's Java 8 JDK download page with the licence agreement requirement.
Oracle’s Java 8 JDK download page with the licence agreement requirement.

The problem is, as you can imagine, you will have troubles to download it from the command line. You have three options:

  1. Download the file from a command-line web browser
  2. Download the file from a different device which has a GUI web browser and copy it to your machine
  3. Cheat the Oracle’s web server

The third is my favorite approach and is the one I will write about.

Downloading the installation file

What the Oracle’s download page does, is to create a cookie in the browser when the user selects “Accept Licence Agreement”. This cookie is sent to the server as a HTTP Header parameter when the file is requested. But, because we are not using a browser to request the file, we will have to do it in another way which allow us to send the header information. One way is to use the wget command. This command is a free utility for non-interactive download of files from the Web. It supports HTTPHTTPS, and FTP protocols, as well as retrieval through HTTP proxies [3]. It has a parameter, –header, that adds header information to the request. Given this information, all we need to do is to run the following command to download the Java 8 JDK:

WARNING: By using this method, you are still agreeing with the Oracle Binary Code License Agreement for the Java SE.

wget -c --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/8u5-b13/jdk-8u5-linux-x64.rpm" --output-document="jdk-8u5-linux-x64.rpm"

If there is a new version available, just replace the 8u5-b13/jdk-8u5-linux-x64.rpm in the address and jdk-8u5-linux-x64.rpm in the output-document (just to be consistent) with the correct version. Also, if you are using a x86 processor architecture, the file name is (in this version’s case) jdk-8u5-linux-i586.rpm.

Installing it

Now that we have downloaded the file (via the method I mentioned above or via browser), let’s get started with the installation. The only thing you need to do is to run the following command:

sudo rpm -i jdk-8u5-linux-x64.rpm

Done? Great, now we need to register the environment variable JAVA_HOME and define the command java to the new installation (in case you have other versions installed, such as OpenJDK or an older Oracle’s Java version).

sudo alternatives --install /usr/bin/java java /usr/java/default/bin/java 20000
export JAVA_HOME=/usr/java/default

That’s all! Check with the following command if your new installation is ready (verify the printed version):

java -version

Still not showing you the right version?

Josep and Rui Brito had troubles and kindly shared their additional step to make it work.

Josep‘s solution was to remove the current java default version before setting the new one.

sudo alternatives --remove java/usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java

sudo alternatives --install /usr/bin/java java /usr/java/default/bin/java 20000

Rui Brito’s solution was to select one of the java versions available.

sudo alternatives --install /usr/bin/java java /usr/java/default/bin/java 20000

echo 2 | /usr/sbin/alternatives --config java

Both alternatives make sense. Try one or the other if you had troubles setting the java version.

Java slave links

As Alexandre Sieira pointed out, there are some libraries that are not linked with the alternatives method above described. If you need to use any of those libraries and/or if you intent to compile your code on an EC2 instance, you can use the commands described in this article: [4]

Didn’t it work for you? Do you have an easier or better way to do it? Share it with us. Thank you Alexandre Sieira, Josep and Rui Brito for your contribution.

Stay tuned and thank you for coming!

Sources:

  1. Glassfish 4 release notes
  2. JDK Installation for Linux Platforms
  3. wget – Linux Command
  4. Linux Alternatives and Oracle Java

Published in Java 8 Programming

13 Comments

  1. dave dave

    I am adding this late but was searching for how this is done now as I havent done it for a while. Alternatives is painful for setting java versions so I recommend avoiding alternatives completely.

    Why not uninstall all java versions, then download the tar file for the JDK versions you want to use. Untar each tarball under /usr/lib/jvm/ and use JAVA_HOME to set the specific version of java to use as well as adding $JAVA_HOME/bin to the PATH.

    I set JAVA_HOME and add $JAVA_HOME/bin to the PATH in one of 3 places:

    > I create /etc/profile.d/java.sh to set the global location for java when I only have one version installed.

    > Within the .bash_profile, for a user

    > In the startup script for the application that is running.

    This means we don’t have to fiddle with the alternatives rubbish. alternatives just makes it harder to manage multiple versions of java.

  2. Alexandre Sieira Alexandre Sieira

    At the very least you should supply all the Java commands as slaves:

    sudo alternatives –install /usr/bin/java java /usr/java/default/bin/java
    –slave /usr/bin/keytool keytool /usr/java/default/bin/keytool
    –slave /usr/bin/orbd orbd /usr/java/default/bin/orbd
    –slave /usr/bin/pack200 pack200 /usr/java/default/bin/pack200
    –slave /usr/bin/rmid rmid /usr/java/default/bin/rmid
    –slave /usr/bin/rmiregistry rmiregistry /usr/java/default/bin/rmiregistry
    –slave /usr/bin/servertool servertool /usr/java/default/bin/servertool
    –slave /usr/bin/tnameserv tnameserv /usr/java/default/bin/tnameserv
    –slave /usr/bin/unpack200 unpack200 /usr/java/default/bin/unpack200
    20000

    • Sorry, I replied to your last comment without realising that you wrote down the solution already. Thank you!

      Anyway, the article I mentioned shows which slave links should be declared also for javac 😉 might be useful.

      Best regards!

  3. Alexandre Sieira Alexandre Sieira

    Actually, if you run `alternatives –display java` you will notice that the OpenJDK 1.7 RPM has several slave links declared which your command above didn’t provide.

    That might create issues when compiling programs that require the libs, for example. This is the state after your recommendations above:

    /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java – prioridade 17000
    keytool slave: /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/keytool
    orbd slave: /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/orbd
    pack200 slave: /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/pack200
    rmid slave: /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/rmid
    rmiregistry slave: /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/rmiregistry
    servertool slave: /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/servertool
    tnameserv slave: /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/tnameserv
    unpack200 slave: /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/unpack200
    jre_exports slave: /usr/lib/jvm-exports/jre-1.7.0-openjdk.x86_64
    jre slave: /usr/lib/jvm/jre-1.7.0-openjdk.x86_64
    java.1.gz slave: /usr/share/man/man1/java-java-1.7.0-openjdk.1.gz
    keytool.1.gz slave: /usr/share/man/man1/keytool-java-1.7.0-openjdk.1.gz
    orbd.1.gz slave: /usr/share/man/man1/orbd-java-1.7.0-openjdk.1.gz
    pack200.1.gz slave: /usr/share/man/man1/pack200-java-1.7.0-openjdk.1.gz
    rmid.1.gz slave: /usr/share/man/man1/rmid-java-1.7.0-openjdk.1.gz
    rmiregistry.1.gz slave: /usr/share/man/man1/rmiregistry-java-1.7.0-openjdk.1.gz
    servertool.1.gz slave: /usr/share/man/man1/servertool-java-1.7.0-openjdk.1.gz
    tnameserv.1.gz slave: /usr/share/man/man1/tnameserv-java-1.7.0-openjdk.1.gz
    unpack200.1.gz slave: /usr/share/man/man1/unpack200-java-1.7.0-openjdk.1.gz
    /usr/java/default/bin/java – prioridade 20000
    keytool slave: (null)
    orbd slave: (null)
    pack200 slave: (null)
    rmid slave: (null)
    rmiregistry slave: (null)
    servertool slave: (null)
    tnameserv slave: (null)
    unpack200 slave: (null)
    jre_exports slave: (null)
    jre slave: (null)
    java.1.gz slave: (null)
    keytool.1.gz slave: (null)
    orbd.1.gz slave: (null)
    pack200.1.gz slave: (null)
    rmid.1.gz slave: (null)
    rmiregistry.1.gz slave: (null)
    servertool.1.gz slave: (null)
    tnameserv.1.gz slave: (null)
    unpack200.1.gz slave: (null)

  4. John John

    Great Post!! Help a lot!!
    Thank you!!

  5. Rui Brito Rui Brito

    Another excellent post . Very useful!!
    I just have one thing to add that happened in my case.
    Because I used a bash file to configure my EC2 instances when I launch then, I need to after the:


    sudo alternatives --install /usr/bin/java java /usr/java/default/bin/java 20000

    To insert:

    echo 2 | /usr/sbin/alternatives --config java

    So that I can select that version of java, if you find it interesting, you can add it as a (special) case in the post for future reference.

    • Thank you for your contribution! Updated the post with your additional step.

  6. Josep Josep

    Didn’t completely work on my installation.

    I have removed the previous Java alternative. Adding the following command, and now works fine:

    sudo alternatives --remove java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java

    Your package might be different. You can list those here:

    sudo alternatives –display java

    • Thank you for your contribution! Updated the post with your additional step.

Leave a Reply

Your email address will not be published. Required fields are marked *