How to install Java on your Amazon EC2 instance?

How to install Java on your Amazon EC2 instance?

Last weekend, I was planning to load test an application. Instead of giving tough time to my machine 💻, i decided to setup JMeter on a EC2 instance on AWS. Before installing JMeter, i needed to install Java on the instance.

This post is for anyone trying to install Java on the AWS EC2 instance.

First things first, login in to your Amazon AWS console and search for EC2. Click on the EC2 service. On the dashboard, Click Launch Instance.

No alt text provided for this image


Choose your specifications for the instance as needed and launch your instance. Also, make sure to download the .pem file during the final steps. You will need the .pem to connect to your EC2 instance from your terminal. Configuration of the EC2 instance is pretty easy and you can search for resources on the web, if needed.

Connect to your EC2 instance from your terminal using the .pem file. Go to the download location of the pem file and change the file permission before connecting to the EC2.

> 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 thing, you need to be a super user.

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

Also, you need to 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. Boom 😎, you now have Java installed on your AWS Instance 🔥

To view or add a comment, sign in

More articles by Manzeel Uprety

  • Social Engineering

    Social Engineering is one aspect of Ethical hacking. It involves exploring flaws in humans; how are we as people…

    2 Comments
  • Penetration Testing 101: A Key to Safeguarding Client's Data

    Have you ever thought about how much a single error can cost? Let’s say, we are talking of a data breach. According to…

  • API Testing: What ? Why ? How ?

    What is an API ? If you are someone in the IT field, you must have definitely heard of API. According to Wikipedia, an…

    2 Comments
  • Blue-Green Deployment

    Blue-Green Deployment is a release technique that reduces downtime and risk by running two identical production…

    1 Comment

Others also viewed

Explore content categories