How to Install Java 17 on Amazon Linux

Equipping your Amazon Linux system with Java 17 is a crucial step to Install Java 17 on Amazon Linux and unlock the potential of modern Java applications. This streamlined guide simplifies the installation process, getting you up and coding in no time.

Steps to Install Java 17 on Amazon Linux

Why Java 17 on Amazon Linux?

Java 17 boasts a wealth of advantages, including performance enhancements, improved garbage collection, and valuable features like sealed classes and records. When you combine this with the robust foundation of Amazon Linux 2 or 2023, you gain a powerful and dependable platform that fuels your development endeavors.

Understanding JRE vs. JDK:

Before we dive in, it’s essential to grasp the two main Java components:

  • JRE (Java Runtime Environment): This allows you to execute Java applications.
  • JDK (Java Development Kit): If you’re not just running apps but also building them in Java, you’ll need the full JDK. It includes the JRE plus development tools like compilers and debuggers.

Installing Java 17 with yum (Recommended):

The recommended method for installing Java on Amazon Linux is using the yum package manager. Here’s a breakdown for both Amazon Linux 2 and 2023:

Amazon Linux 2:

1. (Optional) Enable the Amazon Corretto Repository: While recent Amazon Linux 2 releases (generally 2022 and later) might include the Corretto repository by default, this step ensures the latest version. Use this command:

sudo amazon-linux-extras enable corretto8

2. Install Java 17 JRE or JDK:

JRE:

sudo yum install java-17-amazon-corretto

JDK:

sudo yum install java-17-amazon-corretto-devel

Amazon Linux 2023:

Important Note: Amazon Linux 2023 no longer includes separate OpenJDK packages by default. The Amazon Corretto repository is the recommended source for Java.

1. Enable the Amazon Corretto Repository:

sudo amazon-linux-extras enable corretto8

2. Install Java 17 JRE or JDK:

JRE:

sudo yum install java-17-amazon-corretto

JDK:

sudo yum install java-17-amazon-corretto-devel

Verification:

Once the installation is complete, let’s verify it’s working:

java -version

This should display “openjdk version 17” or similar, confirming successful Java 17 installation.

When to Update Alternatives:

  • Manual Java Installation: If you manually installed Java outside of the yum package manager, you might need to update alternatives to ensure the system links to the correct Java version.
  • Multiple Java Versions: If you have multiple Java versions installed through different methods (e.g., manual, yum), updating alternatives can help you choose the default version for the system.

Why yum Takes Care of It:

  • When you install Java 17 using yum install java-17-amazon-corretto or yum install java-17-amazon-corretto-devel, the yum package manager automatically handles updating alternatives. It sets the newly installed Java version as the default.
  • yum keeps track of installed packages and their dependencies. When you install a new Java version, it updates the alternatives accordingly.

Optional: Setting Environment Variables:

For easier access from the command line, consider setting environment variables to point to the Java installation directory. The specific instructions depend on whether you want the change to be temporary or permanent.

Refer to configure the JAVA_HOME Environment Variable (Permanent)

Steps to Update the Java alternatives 

Here are the steps on how to update Java alternatives on Amazon Linux, in case you have a scenario where it’s necessary:

1. Check Installed Java Versions:

Use the following command to see which Java versions are currently installed on your system:

update-alternatives --list java

This will output something like this, showing available Java alternatives with the currently selected one marked with an asterisk (*):

java /usr/lib/jvm/java-11-amazon-corretto/bin/java current java /usr/lib/jvm/java-17-amazon-corretto/bin/java

2. Select the Desired Java Version:

Use the following command to set the desired Java version as the default system-wide Java:

update-alternatives --config java

This will display a list of available Java versions with numbers assigned. Enter the number corresponding to the Java version you want to make the default, for example:

Enter the path to provide for the alternative (or press enter to keep the current): /usr/lib/jvm/java-17-amazon-corretto/bin/java

3. Verify the Change:

Run the following command to confirm that the desired Java version is now the default:

update-alternatives --list java

This should show the chosen Java version as the current default with an asterisk (*).

Additional Notes:

  • These steps assume you have installed Java versions using the update-alternatives method. If you installed using yum, it generally handles alternatives updates automatically.
  • Be cautious when changing the default Java version, as it might affect applications that rely on a specific version.
  • For more advanced usage or troubleshooting, refer to the official documentation for update-alternatives on Amazon Linux: https://docs.aws.amazon.com/linux/al2023/ug/managing-repos-os-updates.html

Refer to Test the Java installation

Conclusion:

With Java 17 up and running on your Amazon Linux system, you’re ready to take advantage of its latest features and capabilities.

See also:

How to Upgrade Java 16 To Java 17 on Rocky Linux 8 or AlmaLinux 8

How to Upgrade Java 11 To Java 16 on Rocky Linux or AlmaLinux

Steps to Upgrade Java 8 to Java 11 on CentOS 7

How to Upgrade Java 8 To Java 11 on Rocky Linux or AlmaLinux

How to Install Java 8 on Rocky Linux 8 / CentOS 8 / RHEL 8: A Step-by-Step Guide

How to Upgrade Java 8 to Java 11 on Ubuntu 20

How to Install Java 8 on Ubuntu 20

List of monitoring tools 

Linux Blogs

AWS Cloud Blogs

Database Blogs

DevOps Blogs

Interview Questions & Answers

Docker Blogs

Google Cloud Blogs