Install Configure JDK in Fedora 42
Fedora 42 ships with bleeding-edge tech, but if you’re a Java developer who wants to run the latest LTS version (Java 21)—or switch between Oracle JDK and OpenJDK—you’ll need to get your hands a bit dirty.
In this guide, we’ll cover:
- ✅ Installing OpenJDK 21 using DNF (Fedora’s package manager)
- ✅ Installing Oracle JDK 21 manually from Oracle’s site
- ✅ Switching between Java versions using the
alternatives
command
Let’s dive in.
🧰 Prerequisites
Make sure you’re running Fedora 42, and you’ve got sudo
privileges. Open your terminal and run:
cat /etc/fedora-release
You should see something like:
Fedora release 42 (Adams)
🔧 Install OpenJDK 21 using DNF
Fedora’s default repositories offer OpenJDK 21. Run:
sudo dnf install java-21-openjdk java-21-openjdk-devel
Verify installation:
java -version
Expected output:
openjdk version "21" ...
☕ Install Oracle JDK 21 (Manual)
Oracle JDK isn’t in Fedora’s repos due to licensing, so we’ll do a manual install.
Step 1: Download Oracle JDK 21
Go to Oracle’s official JDK page: 👉 https://www.oracle.com/java/technologies/javase/jdk21-archive-downloads.html
Download the RPM file
Step 2: Extract and Move to /opt
cd ~/Downloads
sudo rpm -Uhv jdk*-linux-x64_bin.rpm
(replace actual file name)
🔄 Switching Between Java Versions with alternatives
Fedora uses alternatives
to manage multiple versions of the same tool.
Use alternatives
to Choose Your Default
To select which Java runtime you want:
sudo alternatives --config java
You’ll see:
There are 2 programs which provide 'java'.
Selection Command
-----------------------------------------------
*+ 1 /usr/lib/jvm/java-21-openjdk/bin/java
2 /opt/jdk-21/bin/java
Enter to keep the current selection[+], or type selection number:
Choose the one you want. Repeat the same for javac
:
sudo alternatives --config javac
✅ Verify It Works
java -version
javac -version
You should now see whichever JDK you selected as the output.
📌 Pro Tip: Set JAVA_HOME
in .bashrc
or .zshrc
Add this to your shell config to avoid issues with Maven, Gradle, etc.
For Oracle JDK:
export JAVA_HOME=/opt/jdk-21
export PATH=$JAVA_HOME/bin:$PATH
For OpenJDK:
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk
export PATH=$JAVA_HOME/bin:$PATH
Then:
source ~/.bashrc # or ~/.zshrc
🚀 Wrapping Up
You now have both OpenJDK 21 and Oracle JDK 21 installed on Fedora 42, and you know how to switch between them cleanly using alternatives
.
If you’re working on different projects or enterprise apps that require Oracle JDK, this setup gives you flexibility without compromising the Fedora way.