如何在AWS AMI EC2实例上安装JDK 17?请提供对应的安装命令
Hey there! Installing JDK 17 on an AWS EC2 AMI instance is totally straightforward—let's break it down step by step, since the process varies a bit depending on which Amazon Linux version your AMI is running (most folks use either Amazon Linux 2 or Amazon Linux 2023).
Installing JDK 17 on Amazon Linux 2
- First, update your system's package index to make sure you're pulling the latest versions:
sudo yum update -y - Next, install Amazon Corretto 17—this is AWS's official, open-source JDK optimized for EC2, so it's our top recommendation:
If you prefer using OpenJDK instead, you can run this command instead:sudo yum install -y amazon-corretto-17sudo yum install -y java-17-openjdk-devel - Verify the installation worked by checking the Java version:
You should see output showing version 17.x.x.java -version javac -version - (Optional) Set the
JAVA_HOMEenvironment variable so your system and applications can find the JDK easily:- Open your bash profile with a text editor (like nano):
nano ~/.bash_profile - Add this line at the end (adjust the path if your JDK is installed elsewhere—you can locate it with
find /usr/lib/jvm -name "java-17*"):export JAVA_HOME=/usr/lib/jvm/java-17-amazon-corretto.x86_64 - Save and exit (for nano: Ctrl+O, Enter, then Ctrl+X), then reload the profile to apply changes:
source ~/.bash_profile - Confirm it's set correctly:
echo $JAVA_HOME
- Open your bash profile with a text editor (like nano):
Installing JDK 17 on Amazon Linux 2023
Amazon Linux 2023 uses dnf instead of yum for package management, so the steps are slightly different:
- Start by updating your system packages:
sudo dnf update -y - Install Amazon Corretto 17 (again, the recommended option):
For OpenJDK, use this command instead:sudo dnf install -y amazon-corretto-17sudo dnf install -y java-17-openjdk-devel - Verify the installation:
java -version javac -version - (Optional) Set
JAVA_HOMEjust like we did for Amazon Linux 2:- Open your bash profile:
nano ~/.bash_profile - Add the export line (check the path with
find /usr/lib/jvm -name "java-17*"if needed):export JAVA_HOME=/usr/lib/jvm/java-17-amazon-corretto.x86_64 - Save, exit, and reload the profile:
source ~/.bash_profile - Confirm the variable is set:
echo $JAVA_HOME
- Open your bash profile:
That's all! You should have JDK 17 up and running on your EC2 instance in no time.
内容的提问来源于stack exchange,提问作者oOXAam
相关产品推荐
相关产品推荐

