Install Java on Amazon Linux 2023: Corretto 21 or 25, Why "dnf install java" Picks a Short-Lived Release, and Switching java and javac
To install Java on Amazon Linux 2023, pick a long-term-support version and name it: sudo dnf install -y java-21-amazon-corretto-devel for Java 21, or java-25-amazon-corretto-devel for Java 25. AL2023 has no openjdk packages; its Java is Amazon Corretto, AWS's free build of OpenJDK. Here's why you should name the version: plain sudo dnf install java picks the newest Corretto in the repository, and on the October 2026 release of AL2023 that's Corretto 26, a short-lived feature release whose support ends in October 2026. It also installs only the runtime, with no javac. This page shows every Java version AL2023 offers with its end-of-life date, how to switch versions with alternatives (and why you have to switch java and javac separately), the UnsupportedClassVersionError that a mix of versions causes, JAVA_HOME, and why dnf install maven brings yet another Java, version 17.
Ethan, a developer, needed a small Java service on a new EC2 server for his friend Jake's phone repair shop, to send repair status texts to customers. He ran sudo dnf install java, saw "Complete!", and moved on. A week later he noticed which version he'd actually got.
New to EC2? Amazon EC2 is a server you rent by the hour, and Amazon Linux 2023 is AWS's own Linux for it. Java doesn't come preinstalled; you add the version you need with dnf.
What "dnf install java" really installs
Here's what Ethan got:
sudo dnf install -y java
java -version
openjdk version "26.0.2.1" 2026-08-18
OpenJDK Runtime Environment Corretto-26.0.2.11.1 (build 26.0.2.1+11-FR)
OpenJDK 64-Bit Server VM Corretto-26.0.2.11.1 (build 26.0.2.1+11-FR, mixed mode, sharing)
Look at the build string: FR, for feature release. Java has two kinds of release. Long-term-support (LTS) versions, such as 21 and 25, get security updates for years. Feature releases come out every six months between them and get updates for only a few months. Corretto 26's last planned update was in July 2026, and it reaches end of life in October 2026. So the command that looks like "install Java" gave Ethan a version with weeks of support left.
It also gave him only half of Java. The java package is the runtime, for running programs. To compile, you need the -devel package, and without it:
javac -version
bash: javac: command not found
Jake: "So 'install Java' picked the newest one, and the newest one is about to expire?"
Ethan: "Newest isn't the same as supported. The supported ones are the ones with LTS in the version string."
Every Java version Amazon Linux 2023 offers
AL2023's repository carries Corretto packages for many Java versions, including some whose support has already ended. Here's the list on the October 2026 release, with each version's end of life from the Corretto support calendar:
| Package prefix | Version on AL2023 | Type | End of life |
|---|---|---|---|
java-25-amazon-corretto | 25.0.4 | LTS | October 2032 |
java-21-amazon-corretto | 21.0.12 | LTS | October 2030 |
java-17-amazon-corretto | 17.0.20 | LTS | October 2029 |
java-11-amazon-corretto | 11.0.32 | LTS | January 2032 |
java-1.8.0-amazon-corretto | 1.8.0_504 | LTS | December 2030 |
java-26-amazon-corretto | 26.0.2 | Feature release | October 2026 |
java-24-amazon-corretto | 24.0.2 | Feature release | Ended October 2025 |
java-23-amazon-corretto | 23.0.2 | Feature release | Ended April 2025 |
If you're starting a new project, Java 21 or 25 is your safe choice. AL2023 itself is built around 17: every Java-based package in AL2023 is built with Corretto 17, which matters for Maven, below. To see what your server offers today, run dnf list --available "java-*-amazon-corretto*".
Install Java 21 or 25: headless, full or devel
Each version comes in three main packages, and you pick by what your server does:
| Package | What you get | Pick it when you |
|---|---|---|
java-21-amazon-corretto-headless | The runtime, without desktop libraries | Only run Java programs on a server |
java-21-amazon-corretto | The runtime, with desktop libraries | Run programs that draw images or windows |
java-21-amazon-corretto-devel | The full JDK: javac and the other tools, plus the headless runtime | Build or compile code on the server |
If you build code on your server, install the devel package:
sudo dnf install -y java-21-amazon-corretto-devel
java -version
javac -version
openjdk version "21.0.12.1" 2026-08-18 LTS
OpenJDK Runtime Environment Corretto-21.0.12.12.1 (build 21.0.12.1+12-LTS)
OpenJDK 64-Bit Server VM Corretto-21.0.12.12.1 (build 21.0.12.1+12-LTS, mixed mode, sharing)
javac 21.0.12.1
This time the version string says LTS. For Java 25, replace 21 with 25 in the package name. If the build or test tools you use need an older version, 17 and 11 are there too, and you can install several versions side by side.
Switch versions: java and javac are switched separately
With more than one version installed, AL2023 uses the alternatives system to decide which one the plain java command runs. By default, it picks the highest version. To choose, run the interactive menu:
sudo alternatives --config java
Choosing in that menu, even pressing Enter to keep the current version, switches java from automatic to manual mode, so a newer version installed later won't take over; sudo alternatives --auto java turns automatic mode back on.
Here's the catch: java and javac are two separate choices. Switch one, and the other stays where it was. Ethan installed the 21 JDK next to the 26 runtime, and ended up with this:
alternatives --list | grep -E "^java |^javac "
java auto /usr/lib/jvm/java-26-amazon-corretto.x86_64/bin/java
javac auto /usr/lib/jvm/java-21-amazon-corretto.x86_64/bin/javac
To set both in a script, without the menu, use --set with the full paths:
sudo alternatives --set java /usr/lib/jvm/java-21-amazon-corretto.x86_64/bin/java
sudo alternatives --set javac /usr/lib/jvm/java-21-amazon-corretto.x86_64/bin/javac
Or remove the version you don't want, and alternatives falls back to the next one on its own. If your server got Corretto 26 by accident, this is the cleanest way back:
- Install the LTS JDK you want first, for example
java-21-amazon-corretto-devel, so your server is never without Java. - Remove every Corretto 26 package, as shown below.
- Check that
java -versionandjavac -versionnow both report 21. - Point
JAVA_HOMEat the 21 folder, if your tools use it. - Restart your Java services, so they pick up the version you chose.
The removal itself, and what java reports afterward:
sudo dnf remove -y "java-26-amazon-corretto*"
java -version
openjdk version "21.0.12.1" 2026-08-18 LTS
OpenJDK Runtime Environment Corretto-21.0.12.12.1 (build 21.0.12.1+12-LTS)
OpenJDK 64-Bit Server VM Corretto-21.0.12.12.1 (build 21.0.12.1+12-LTS, mixed mode, sharing)
UnsupportedClassVersionError: when java and javac disagree
A mixed setup is how you get the most confusing Java error on AL2023. Compile with a newer javac, run with an older java, and the program won't start:
Error: LinkageError occurred while loading main class Hello
java.lang.UnsupportedClassVersionError: Hello has been compiled by a more recent version of the Java Runtime (class file version 70.0), this version of the Java Runtime only recognizes class file versions up to 65.0
The numbers name the versions: class file version 70 is Java 26, and 65 is Java 21. So this program was compiled by Java 26 and run on Java 21. Two ways to fix it:
- Switch
javacto the same version asjava, withalternatives --setas above, and compile again. - Or tell the compiler which version to build for:
javac --release 21 Hello.java. The class then runs on Java 21 and anything newer. - In Maven or Gradle builds, set the same target,
maven.compiler.releaseor Gradle's toolchain, so every build machine produces the same result. - Check what you have with
java -versionandjavac -versiontogether; they should match.
Jake: "So the program was written for a newer Java than the one running it?"
Ethan: "Built by one version, run by another. Same code, but the older one can't read the newer file format."
Setting JAVA_HOME on Amazon Linux 2023
Many of the tools you'll use, such as Tomcat or your build scripts, want JAVA_HOME, the folder Java is installed in. On AL2023, each Corretto version lives under /usr/lib/jvm, and there's a stable link without the processor architecture in its name, so it works on both x86 and Graviton servers:
export JAVA_HOME=/usr/lib/jvm/java-21-amazon-corretto
To find the folder for whatever java runs on your server right now, follow the links from the command:
dirname "$(dirname "$(readlink -f "$(command -v java)")")"
/usr/lib/jvm/java-21-amazon-corretto.x86_64
For a service started by systemd, set it in the unit file with Environment=JAVA_HOME=/usr/lib/jvm/java-21-amazon-corretto, because services don't read your login shell's settings. If the service runs in a container instead, the image chooses its own Java; our ECS memory guide covers the Java settings that matter there.
Why dnf install maven brings Java 17
Install Maven the obvious way, and it brings a third Java version:
sudo dnf install -y maven
mvn -version
Apache Maven 3.8.4 (Red Hat 3.8.4-3.amzn2023.0.5)
Maven home: /usr/share/maven
Java version: 17.0.20.1, vendor: Amazon.com Inc., runtime: /usr/lib/jvm/java-17-amazon-corretto.x86_64
AL2023's Maven package is tied to a Java version through small "binding" packages, and the default binding is Corretto 17, the version AL2023's own Java packages are built with. To run Maven on Java 21, install the binding for it instead of plain maven:
sudo dnf install -y maven-amazon-corretto21
If Maven is already installed with the 17 binding, installing the 21 one directly fails with a conflict, because only one binding can be installed. Swap them in one step:
sudo dnf swap -y maven-amazon-corretto17 maven-amazon-corretto21
mvn -version
Apache Maven 3.8.4 (Red Hat 3.8.4-3.amzn2023.0.5)
Maven home: /usr/share/maven
Java version: 21.0.12.1, vendor: Amazon.com Inc., runtime: /usr/lib/jvm/java-21-amazon-corretto.x86_64
Bindings exist for 8, 11, 17 and 21. The Maven itself is 3.8.4; projects that need a newer Maven usually include the Maven wrapper, mvnw, which downloads the version the project asks for.
"Unable to find a match: java-21-openjdk"
Guides written for Red Hat, Fedora or CentOS install Java as java-21-openjdk. On AL2023 that fails:
sudo dnf install -y java-21-openjdk
No match for argument: java-21-openjdk
Error: Unable to find a match: java-21-openjdk
Corretto is AL2023's OpenJDK, under a different package name: java-21-amazon-corretto. For scripts that expect the Red Hat folder names, the Corretto packages also create links such as /usr/lib/jvm/java-21-openjdk and /usr/lib/jvm/jre-21-openjdk, pointing at the Corretto install, so paths copied from those guides keep working once Corretto is installed.
Troubleshooting by symptom
java -version shows 26 but I installed 21
The higher version wins by default. Run sudo alternatives --config java, or remove Corretto 26.
javac: command not found
Only the runtime is installed. Install the devel package, such as java-21-amazon-corretto-devel.
UnsupportedClassVersionError: class file version 70.0
Compiled by Java 26, run on an older Java. Match javac to java, or compile with --release 21.
No match for argument: java-21-openjdk
AL2023 names its OpenJDK packages Corretto. Install java-21-amazon-corretto instead.
mvn -version shows Java 17
dnf install maven pulls in the Corretto 17 binding. Swap it for maven-amazon-corretto21.
JAVA_HOME is not set in a systemd service
Services don't read shell profiles. Add Environment=JAVA_HOME=... to the unit file, then reload and restart.
Java on Amazon Linux 2023: frequently asked questions
How do I install Java on Amazon Linux 2023?
Run sudo dnf install -y java-21-amazon-corretto-devel for Java 21, or the java-25 package for Java 25.
What does dnf install java install on Amazon Linux 2023?
The newest Corretto runtime in the repository. On the October 2026 release that's Corretto 26, a feature release that reaches end of life in October 2026.
Is OpenJDK available on Amazon Linux 2023?
Yes, as Amazon Corretto, AWS's build of OpenJDK. The packages are named java-21-amazon-corretto and so on, not java-21-openjdk.
Which Java versions are LTS on Amazon Linux 2023?
Corretto 8, 11, 17, 21 and 25. Corretto 23, 24 and 26 are feature releases with short support.
How do I install Java 17 on Amazon Linux 2023?
Run sudo dnf install -y java-17-amazon-corretto-devel, or the -headless package for the runtime only.
How do I switch Java versions on Amazon Linux 2023?
Run sudo alternatives --config java, then sudo alternatives --config javac. They are switched separately.
What is the difference between headless and devel Corretto packages?
Headless is the runtime without desktop libraries. Devel is the full JDK with javac and other development tools.
Where is Java installed on Amazon Linux 2023?
Under /usr/lib/jvm, for example /usr/lib/jvm/java-21-amazon-corretto.x86_64, with a link at /usr/lib/jvm/java-21-amazon-corretto.
How do I set JAVA_HOME on Amazon Linux 2023?
Set JAVA_HOME=/usr/lib/jvm/java-21-amazon-corretto, in your shell profile or in a systemd unit's Environment line.
Why does Maven use Java 17 on Amazon Linux 2023?
dnf install maven installs the maven-amazon-corretto17 binding. Use maven-amazon-corretto21, or dnf swap to change it.
What does class file version 70.0 mean?
The class was compiled for Java 26. Version 65 is Java 21 and 69 is Java 25.
How do I fix UnsupportedClassVersionError on Amazon Linux 2023?
Run the program with a Java at least as new as the compiler, or compile with javac --release for the older version.
Can I install multiple Java versions on Amazon Linux 2023?
Yes. Each Corretto version installs to its own folder, and alternatives chooses which one java and javac run.
Is Corretto free to use in production?
Yes. Corretto is a no-cost OpenJDK distribution from AWS, with updates for LTS versions for years.
Should I use Java 21 or Java 25 on Amazon Linux 2023?
Both are LTS. Java 25 is supported longer, to October 2032; Java 21 to October 2030. Match your frameworks' support.
How do I remove a Java version from Amazon Linux 2023?
Run sudo dnf remove "java-26-amazon-corretto*" with the version's prefix. alternatives then switches to the next version.
For readers preparing for an AWS certification
We have been covering AWS certifications alongside our fixes, so here is a short extra for readers studying for one. If you only came for the fix, you already have it; feel free to skip this part.
Exams that test this topic: CloudOps Engineer – Associate (SOA-C03) • Developer – Associate (DVA-C03 from December 1, 2026).
Here are the exam-style questions this topic tends to raise, with the reasoning you need to answer them:
1. Which Java distribution does AWS provide?
Amazon Corretto, a no-cost, long-term supported OpenJDK distribution.
2. How do you switch between installed Java versions?
With alternatives --config java.
3. Which Lambda feature speeds up Java cold starts?
Lambda SnapStart.
4. How do you run Java apps with managed scaling and deployment?
Elastic Beanstalk, ECS or EKS, or Lambda for event-driven code.
5. How do you profile a Java app running on AWS?
Amazon CodeGuru Profiler or Application Signals in CloudWatch.
6. Which instances often give better price-performance for Java?
Graviton (arm64) instances, with Corretto builds for arm64.
Exam codes and dates change often. Our AWS certification changes page keeps track of what is current.
Ethan removed Corretto 26, installed the Java 21 JDK and the Maven binding for 21, and set JAVA_HOME in the service's unit file. The status-text service builds and runs on one version from end to end. Jake asked how he'd know when it's time to upgrade again. Ethan wrote one date on the shop's whiteboard: October 2030, the end of Java 21's support.
📌 If you keep one line from this page
On Amazon Linux 2023, install Java by name, java-21-amazon-corretto-devel or the 25 version, never plain java, and switch java and javac together.
LTS = 8, 11, 17, 21, 25 · alternatives --config java + javac · maven-amazon-corretto21 · JAVA_HOME=/usr/lib/jvm/java-21-amazon-corretto.
Revision note. Written October 13, 2026, on Amazon Linux 2023 release 2023.12.20261012. "All that glitters is not gold," as the proverb goes; the newest Java in the repository isn't always the one to build on.