Skip to content

How to install Java on your EC2 instance?

Published:ย atย 01:16 PM

Last weekend, I was gearing up to load test an app. Instead of stressing out my laptop ๐Ÿ’ป, I decided to spin up JMeter on an AWS EC2 instance. Before diving into JMeter, I needed to get Java installed on the instance.

This post is for anyone looking to get Java up and running on an AWS EC2 instance.

First things first, log in to your Amazon AWS console and hunt down EC2. Click on the EC2 service, then hit Launch Instance on the dashboard. ๐Ÿš€
ec2-instance
Choose your instance specs based on your needs and hit that Launch Instance button. ๐Ÿš€ Donโ€™t forget to download the .pem file during the final stepsโ€”itโ€™s your golden ticket to connect to your EC2 instance from your terminal. ๐Ÿ’ป

To connect, head to the download location of the .pem file and tweak the file permissions before making the connection. This partโ€™s a breeze, but if you need a hand, plenty of resources are available online. ๐ŸŒ๐Ÿ”ง

> chmod 400 mypemfile.pem

After changing the file permission, connect to you EC2 instance from your terminal.

> ssh -i mypemfile.pem ec2-user@xx.x.xx.xxx

During the SSH process, make sure you are in the same directory where your pem file resides. First, you need to be a super user.

[ec2-user@ip-xxx-xx-xx-xxxx ~]$ sudo su

Also, make sure everything is up to date on the server.

[ec2-user@ip-xxx-xx-xx-xxxx ~]$ yum update

By default, the EC2 instance does not have Java installed. You can check it using the command in the EC2 console.

[ec2-user@ip-xxx-xx-xx-xxxx ~]$ java -version

Now, create a sub-directory named jvm inside /usr/lib/jvm. You can access this folder by accessing the basic file system using cd .. two times.

[ec2-user@ip-xxx-xx-xx-xxxx /]$ cd ..
[ec2-user@ip-xxx-xx-xx-xxxx /]$ cd ..

You need to change the file permission of the jvm folder. Go to your /usr/lib/ directory and type in the following command.

[ec2-user@ip-xxx-xx-xx-xxxx lib]$ chmod u=rwx,g=rwx,o=rwx jvm

Now, you need to upload the jdk file to this jvm folder from your machine. Exit from the EC2 terminal and copy the downloaded jdk file to your instance using the following command.

> sudo scp -i mypemfile.pem jdk-16.0.2_linux-x64_bin.tar.gz ec2-user@x.xx.xxx.xxx:/usr/lib/jvm/

Depending upon you internet speed, it will take up to a few minutes. Now you can connect back to your instance and navigate to your jvm folder. Unzip the tar file using the following command. Hit Enter.

[ec2-user@ip-xxx-xx-xx-xxxx jvm]$ tar zxvf jdk-16.0.2_linux-x64_bin.tar.gz

Now, you need to add the environmental variables so that the linux installation knows where our java resides. As a super user (sudo su), you need to modify the environment file in the /etc/ folder.

[ec2-user@ip-xxx-xx-xx-xxxx jvm]$ sudo su
[ec2-user@ip-xxx-xx-xx-xxxx jvm]$ nano /etc/environment

On the Nano editor, add the following path.

PATH= โ€œ/usr/lib/jvm/jdk-jdk-16.0.2/binโ€
JAVA_HOME= โ€œ/usr/lib/jvm/jdk-jdk-16.0.2โ€

Save the changes using, ctrl-x and press yes and hit enter.

Now, you need to update the system about the java information (java) and java compile (javac)

[ec2-user@ip-xxx-xx-xx-xxxx jvm]$ update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-16.0.2/bin/java" 0
[ec2-user@ip-xxx-xx-xx-xxxx jvm]$ update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-16.0.2/bin/javac" 0

Now, you can check the Java version on your server with java -version
Boom ๐Ÿ˜Ž, you now have Java installed on your AWS Instance ๐Ÿ”ฅ