AWS - How to install java11 on an EC2 Linux machine?
Solution 1
Use one of the OpenJDK distributions:
https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/downloads-list.html
or
https://adoptopenjdk.net/?variant=openjdk11&jvmVariant=hotspot
Solution 2
Another option might be running the following commands:
In order to install java 11:
sudo amazon-linux-extras install java-openjdk11
For java 8 you can try:
sudo yum install java-1.8.0-openjdk
Finally, if you want to switch between java versions run:
sudo alternatives --config java
Solution 3
Sharing my simple working approach
Downloading the JDK
curl -LO https://corretto.aws/downloads/latest/amazon-corretto-11-x64-linux-jdk.tar.gz
note: check the ec2 instance's platform ie., x64, aarch64, x86, aarch32 etc.,because the wrong platform jdk installation will leads to /bin/java: /bin/java: cannot execute binary file
Extracting the JDK to /usr/java/
sudo tar -xvzf amazon-corretto-11-x64-linux-jdk.tar.gz -C /usr/java/
Run the below single line command to change the soft link for java files in (/usr/bin) directory
cd /usr/java/amazon-corretto-*-linux-x64/bin
for i in java javac jfr; do path=$(find `pwd`/$i -type f); echo $path; sudo alternatives --install /usr/bin/$i $i $path 20000 ; sudo update-alternatives --config $i ; done
after the above command execution, there will be a command prompt that will ask you to choose the selection number which represents the file residing path and we should choose the number which refers to path /usr/java/amazon-corretto-*-linux-x64/bin/
The amazon-corretto jdk version may vary based on the timeline that you download. Currently the java version is 11.0.12.7.1
That's it execute java -version
, you can see the java version as
openjdk version "11.0.12" 2021-07-20 LTS
OpenJDK Runtime Environment Corretto-11.0.12.7.1 (build 11.0.12+7-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.12.7.1 (build 11.0.12+7-LTS, mixed mode)
Related videos on Youtube
James M
Updated on July 09, 2022Comments
-
James M almost 2 years
I tried this
Downloading Java JDK on Linux via wget is shown license page instead
but I keep getting a 404 error.
This command "sudo amazon-linux-extras install java-openjdk11" just states that amazon-linux-extras doesnt exist.
-
Inukshuk almost 4 yearsamazon-linux-extras is only available on Amazon Linux 2 (and does work). If you're using a different Linux distribution, then you have to use one of the other suggested options below.
-
-
kennysliding about 3 yearsThis is a faster alternative answer, upvoted!
-
Andrew Norman over 2 yearslove the detail here, thanks!
-
Sealchan about 2 yearsThis works only if you open up outbound https in your Security Group assigned to your EC2 instance! lol
-
Maveňツ almost 2 yearsI need to install java 11 in aws code build virtual machine and its not working for me. OS details Ubuntu 18.04.4 LTS architecture 4.14.275-207.503.amzn2.x86_64