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 .
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 , 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).
The problem is, as you can imagine, you will have troubles to download it from the command line. You have three options:
- Download the file from a command-line web browser
- Download the file from a different device which has a GUI web browser and copy it to your machine
- 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 HTTP, HTTPS, and FTP protocols, as well as retrieval through HTTP proxies . 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.
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):
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: 
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!