Over a million developers have joined DZone.

Installing Java 8 on AWS EC2 Amazon Linux Instance

DZone 's Guide to

Installing Java 8 on AWS EC2 Amazon Linux Instance

Take a quick look at this brief tutorial showing you how to install Java 8 in an Amazon EC2 instance.

· Cloud Zone ·
Free Resource

Today we are going to install Java 8 (JDK 8, I must say from the developer's perspective) in an Amazon EC2 instance which is having an Amazon Linux version installed on it. In a nutshell, we can say that Amazon Linux can be considered equivalent to CentOS 6.

I am not going to waste your time and will instead jump directly towards the commands that you have to execute to install JDK 8 on your new instance. Before we start, please ensure you have the administrative privileges. OK. So, you have to execute the following commands as mentioned below.

Installing Java 8

$ cd /opt

$ sudo wget --no-cookies --no-check-certificate --header "Cookie: %3A%2F%2Fwww.oracle.com%2F; -securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.tar.gz

$ sudo tar xzf jdk-8u151-linux-x64.tar.gz

$ cd jdk1.8.0_151/

$ sudo alternatives --install /usr/bin/java java /opt/jdk1.8.0_151/bin/java 2

$ sudo alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command

*+ 1           /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
   2           /opt/jdk1.8.0_151/bin/java

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

(If you are getting some other options as well, choose the one with "/opt/jdk1.8.0_151/bin/java")

Now let's set alternatives, so that once Java is called, it points to the correct JDK version through following commands:

$ sudo alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_151/bin/jar 2

$ sudoalternatives --set jar /opt/jdk1.8.0_151/bin/jar

$ sudo alternatives --set javac /opt/jdk1.8.0_151/bin/javac

Check Java Version 

$ java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

Setting Environment Variables.

We can set the environment variables either user based or globally. With following approach, we are setting the environment variable globally, so that any user on this machine can access it. 

# vim /etc/profile

export JAVA_HOME=/opt/jdk1.8.0_151
export JRE_HOME=/opt/jdk1.8.0_151/jre
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

Esc + :wq! (To save file)

Congratulations!! You did it!

java ,jdk8 ,installation ,amazon aws ,linux ,ec2 ,shell script ,cloud

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}