How to Set Java Environment Path in Fedora Linux

Setting the Java environment path on Fedora configures the JAVA_HOME variable, which directs development tools and frameworks to the correct Java installation. Build systems like Maven and Gradle, application servers like WildFly and Tomcat, and IDEs such as Eclipse and IntelliJ IDEA depend on this variable to locate the Java compiler, runtime libraries, and JDK utilities. When JAVA_HOME is missing or misconfigured, these tools fail to compile code, execute programs, or resolve dependencies properly.

This guide covers different methods for setting JAVA_HOME permanently on Fedora, from system-wide configuration via /etc/profile.d/ to user-specific configuration via .bashrc. You will learn how to choose between OpenJDK versions, use the alternatives system to manage multiple Java installations, verify that your configuration persists across reboots, and troubleshoot common PATH and environment variable issues.

Understanding Java Versions on Fedora

Fedora provides two main OpenJDK packages in its default repositories:

java-latest-openjdk

  • Rolling package that tracks the most recent OpenJDK release (currently OpenJDK 25)
  • Automatically updates to new major versions as Fedora packages them
  • Best for developers who want access to the latest Java features and language improvements
  • The Java version changes over time (25 today, 26 or 27 in future Fedora releases)

java-21-openjdk

  • Pinned to Java 21 specifically, never changes major versions
  • Receives security updates without major version changes
  • Best when your project requires Java 21 specifically or needs version predictability
  • Stays on Java 21 regardless of what java-latest-openjdk tracks

Both Java 21 and Java 25 are Long-Term Support (LTS) releases with extended maintenance. The key difference is version pinning: java-21-openjdk stays on Java 21 forever, while java-latest-openjdk will eventually track Java 26, 27, and beyond. Choose java-21-openjdk when your project explicitly requires Java 21 or when you need the Java version to remain constant. Choose java-latest-openjdk when you want access to the newest Java features and are comfortable with major version updates. Both packages can be installed simultaneously, and Fedora’s alternatives system manages switching between them.

Install Java on Fedora

Check for an Existing Java Installation

Before installing Java, verify whether it is already present on your system. Open a terminal and run:

java -version

If Java is installed, the output shows version details. If Java is not installed, the shell displays:

bash: java: command not found

If no Java installation is found, proceed with the installation steps below.

Install the Latest OpenJDK (Recommended)

Install the latest OpenJDK development kit using DNF:

sudo dnf install java-latest-openjdk-devel

This installs OpenJDK 25 (the current latest version in Fedora 43). The -devel suffix provides the full development kit including the compiler (javac), debugger, and header files needed for building Java applications.

Install Java 21 (Version-Pinned Alternative)

If your project specifically requires Java 21 or you need version predictability, install the pinned Java 21 package instead:

sudo dnf install java-21-openjdk-devel

You can install both versions simultaneously. When both are installed, Fedora’s alternatives system determines which version runs by default based on priority settings (covered in the version switching section below).

Verify the Java Installation

After installation, confirm that Java is correctly installed and accessible:

java -version

If you installed java-latest-openjdk-devel, the output shows:

openjdk version "25.0.1" 2025-10-21
OpenJDK Runtime Environment (Red_Hat-25.0.1.0.8-3) (build 25.0.1+8)
OpenJDK 64-Bit Server VM (Red_Hat-25.0.1.0.8-3) (build 25.0.1+8, mixed mode, sharing)

If you installed java-21-openjdk-devel, or if both are installed (Java 21 takes priority by default), the output shows:

openjdk version "21.0.9" 2025-10-21
OpenJDK Runtime Environment (Red_Hat-21.0.9.0.10-1) (build 21.0.9+10)
OpenJDK 64-Bit Server VM (Red_Hat-21.0.9.0.10-1) (build 21.0.9+10, mixed mode, sharing)

Also verify the Java compiler is available:

javac -version
javac 21.0.9

Set the Java Environment Path

Understanding JAVA_HOME and the alternatives system: JAVA_HOME points to your Java installation root directory, while Fedora’s alternatives system manages symbolic links in /usr/bin and /usr/sbin for version switching. Most Java tools check JAVA_HOME first to locate the JDK, then use binaries like javac and jar from $JAVA_HOME/bin. The alternatives system allows switching between multiple installed Java versions without editing JAVA_HOME manually when using the dynamic configuration methods.

What Is JAVA_HOME?

JAVA_HOME is an environment variable that stores the path to your Java installation directory. Build automation tools like Maven and Gradle, application servers like WildFly, and IDEs rely on this variable to locate the Java compiler, runtime libraries, and other JDK components. When JAVA_HOME is correctly configured, these applications function without requiring manual path specification in every configuration file.

Locate the Java Installation Path

Fedora uses the alternatives system to manage Java installations. To list all installed Java versions and their paths, run:

alternatives --display java

With both Java versions installed, the output shows all Java installations registered with alternatives:

java - status is auto.
 link currently points to /usr/lib/jvm/java-21-openjdk/bin/java
/usr/lib/jvm/java-latest-openjdk/bin/java - priority 1
 follower jre: /usr/lib/jvm/java-latest-openjdk
 follower keytool: /usr/lib/jvm/java-latest-openjdk/bin/keytool
 ...
/usr/lib/jvm/java-21-openjdk/bin/java - priority 21000910
 follower jre: /usr/lib/jvm/java-21-openjdk
 follower keytool: /usr/lib/jvm/java-21-openjdk/bin/keytool
 ...
Current `best' version is /usr/lib/jvm/java-21-openjdk/bin/java.

The output shows that Java 21 has priority 21000910 while java-latest has priority 1. In auto mode, alternatives selects the version with the highest priority number, which is why Java 21 becomes the default when both are installed.

For JAVA_HOME, remove the /bin/java suffix from the path. In this example:

  • Java 21 (pinned): /usr/lib/jvm/java-21-openjdk
  • Java 25 (rolling latest): /usr/lib/jvm/java-latest-openjdk

If alternatives returns an error because no Java packages are installed, first install a JDK using DNF before attempting to configure JAVA_HOME.

Set Temporary JAVA_HOME (Testing)

For testing purposes, you can set JAVA_HOME temporarily for the current terminal session:

export JAVA_HOME="/usr/lib/jvm/java-21-openjdk"

Or for the latest OpenJDK:

export JAVA_HOME="/usr/lib/jvm/java-latest-openjdk"

This temporary setting helps verify that tools recognize JAVA_HOME correctly before making permanent configuration changes. The variable persists only for the current terminal session and disappears when you close the terminal.

Set Permanent JAVA_HOME

Permanent configuration ensures JAVA_HOME persists across terminal sessions and system reboots. Fedora offers multiple methods for setting JAVA_HOME permanently, each suited to different scenarios.

Choose Your Configuration Method

The table below compares the four methods documented in this guide:

MethodScopeTracks AlternativesBest For
/etc/profile.d/ (dynamic)System-wideYes (automatic)Recommended for most users; adapts when you switch Java versions
~/.bashrc (dynamic)User-specificYes (automatic)Per-user configuration that tracks the active Java version
~/.bashrc (static)User-specificNo (manual update)When you need a specific Java version regardless of alternatives
/etc/environmentSystem-wideNo (manual update)Simple static configuration for single-version servers

For most users, the /etc/profile.d/ dynamic method is recommended because it provides system-wide Java configuration that automatically tracks the version selected via the alternatives system. When you switch Java versions using alternatives, JAVA_HOME updates automatically in new terminal sessions.

Method 1: System-Wide Dynamic Configuration via /etc/profile.d/ (Recommended)

This method creates a shell script that automatically detects the currently active Java version through the alternatives system. When you switch Java versions, JAVA_HOME updates automatically in new terminal sessions.

Create the configuration script:

sudo tee /etc/profile.d/java-home.sh > /dev/null <<'EOF'
export JAVA_HOME=$(alternatives --display java 2>/dev/null | grep "link currently points to" | sed "s|.* \(/.*\)/bin/java|\1|")
export PATH="$PATH:$JAVA_HOME/bin"
EOF

This script runs for every user on login, querying the alternatives system to determine the active Java installation path. The 2>/dev/null suppresses error messages if Java is not installed, preventing login errors on systems where Java may be removed.

To apply the changes immediately without logging out:

source /etc/profile.d/java-home.sh

Alternatively, log out and back in for the changes to take effect automatically.

Verify Dynamic Configuration

Check that JAVA_HOME is set correctly:

echo $JAVA_HOME
/usr/lib/jvm/java-21-openjdk

Test that Java runs correctly through JAVA_HOME:

$JAVA_HOME/bin/java -version
openjdk version "21.0.9" 2025-10-21
OpenJDK Runtime Environment (Red_Hat-21.0.9.0.10-1) (build 21.0.9+10)
OpenJDK 64-Bit Server VM (Red_Hat-21.0.9.0.10-1) (build 21.0.9+10, mixed mode, sharing)

Method 2: User-Specific Dynamic Configuration via .bashrc

For user-specific configuration that automatically tracks the alternatives-selected Java version, add the dynamic script to your .bashrc:

nano ~/.bashrc

Add this line at the end of the file:

export JAVA_HOME=$(alternatives --display java 2>/dev/null | grep "link currently points to" | sed "s|.* \(/.*\)/bin/java|\1|")

Save and exit nano (Ctrl+X, then Y and Enter). Apply the changes immediately:

source ~/.bashrc

Alternatively, open a new terminal window for the changes to take effect automatically.

This approach provides the same automatic version tracking as Method 1, but only for your user account rather than system-wide.

Method 3: User-Specific Static Configuration via .bashrc

If you need JAVA_HOME to always point to a specific Java version regardless of the alternatives selection, use a static path in .bashrc:

nano ~/.bashrc

Add these lines at the end of the file:

export JAVA_HOME="/usr/lib/jvm/java-21-openjdk"
export PATH="$PATH:$JAVA_HOME/bin"

Or for the latest OpenJDK:

export JAVA_HOME="/usr/lib/jvm/java-latest-openjdk"
export PATH="$PATH:$JAVA_HOME/bin"

Save and exit nano. Apply the changes:

source ~/.bashrc

With static configuration, you must manually update the path in .bashrc if you want to change the Java version used by JAVA_HOME.

Method 4: System-Wide Static Configuration via /etc/environment

The /etc/environment file provides system-wide environment variables using simple name=value syntax. This method is suitable for servers where the Java version never changes.

sudo nano /etc/environment

Add the following line (note the absence of the export keyword):

JAVA_HOME="/usr/lib/jvm/java-21-openjdk"

Save and exit nano. The /etc/environment file is read by PAM during login, so changes require logging out and back in to take effect.

Important: The /etc/environment file uses simple NAME="value" syntax without the export keyword. Do not add export to entries in this file, as it is parsed as name-value pairs, not executed as a shell script.

Because /etc/environment cannot include shell logic for PATH modifications, you need a separate profile script if you want to add $JAVA_HOME/bin to the system PATH:

echo 'export PATH="$PATH:$JAVA_HOME/bin"' | sudo tee /etc/profile.d/java-path.sh > /dev/null

Switch Between Java Versions

When you have multiple Java versions installed, use the alternatives system to switch between them.

View Available Java Versions

List all installed Java versions:

alternatives --display java

Switch Java Version Interactively

Use the interactive menu to select a Java version:

sudo alternatives --config java

The command displays a numbered list of installed Java versions. Enter the number corresponding to your preferred version and press Enter.

Switch Java Version Directly

To switch to a specific version without the interactive menu:

sudo alternatives --set java /usr/lib/jvm/java-latest-openjdk/bin/java

Or switch to Java 21:

sudo alternatives --set java /usr/lib/jvm/java-21-openjdk/bin/java

Return to Automatic Selection

To let alternatives automatically select the highest-priority Java version:

sudo alternatives --auto java

After switching versions, if you use the dynamic JAVA_HOME methods (Method 1 or 2), open a new terminal to see the updated JAVA_HOME value. If you use the static methods (Method 3 or 4), you must manually update the JAVA_HOME path in your configuration files.

Verify Java Environment Configuration

Validate your configuration by checking the JAVA_HOME and PATH variables:

echo $JAVA_HOME
echo $PATH

The first command displays your Java installation directory:

/usr/lib/jvm/java-21-openjdk

The second command shows your PATH variable, which should include $JAVA_HOME/bin if you added the PATH export:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-21-openjdk/bin

Test that Java commands work correctly through JAVA_HOME:

$JAVA_HOME/bin/java -version
openjdk version "21.0.9" 2025-10-21
OpenJDK Runtime Environment (Red_Hat-21.0.9.0.10-1) (build 21.0.9+10)
OpenJDK 64-Bit Server VM (Red_Hat-21.0.9.0.10-1) (build 21.0.9+10, mixed mode, sharing)

This verification confirms that both JAVA_HOME and PATH are correctly configured, allowing tools like Maven, Gradle, and IDEs to locate Java without issues.

Troubleshooting Common Issues

JAVA_HOME Returns Empty After Configuration

If JAVA_HOME returns empty after editing configuration files, you may have forgotten to reload the configuration or open a new terminal. Check the current value:

echo $JAVA_HOME

If the output is blank, reload the configuration file. For .bashrc changes:

source ~/.bashrc

For /etc/profile.d/ scripts:

source /etc/profile.d/java-home.sh

For /etc/environment changes, log out and back in, as this file is only read during login by PAM.

Verify your configuration file contains the correct JAVA_HOME setting:

grep JAVA_HOME ~/.bashrc
cat /etc/profile.d/java-home.sh
cat /etc/environment

Java Commands Not Found Despite Correct JAVA_HOME

If JAVA_HOME is set correctly but java or javac commands still fail when using $JAVA_HOME/bin/java, your PATH variable may not include $JAVA_HOME/bin. Check if the Java binary directory is in your PATH:

echo $PATH | tr ':' '\n' | grep -i jvm

If there is no output, $JAVA_HOME/bin is missing from PATH. For .bashrc configuration, ensure you added the PATH export line:

grep "PATH.*JAVA_HOME" ~/.bashrc

The output should show:

export PATH="$PATH:$JAVA_HOME/bin"

Java 21 Becomes Default Instead of Latest

When both java-21-openjdk and java-latest-openjdk are installed, Java 21 becomes the default because it has a higher priority number in the alternatives system. Fedora assigns higher priorities to version-pinned packages over the rolling java-latest package.

To use java-latest as your default instead, explicitly set it:

sudo alternatives --set java /usr/lib/jvm/java-latest-openjdk/bin/java

This overrides the priority-based automatic selection and makes java-latest the active version until you change it again.

Do Not Use export in /etc/environment

The /etc/environment file uses simple NAME="value" syntax without the export keyword. If you accidentally added export, remove it:

sudo nano /etc/environment

Change this incorrect format:

export JAVA_HOME="/usr/lib/jvm/java-21-openjdk"

To the correct format:

JAVA_HOME="/usr/lib/jvm/java-21-openjdk"

The /etc/environment file is parsed as name-value pairs by PAM, not executed as a shell script, so the export keyword causes syntax errors or is silently ignored.

Remove Java Configuration

To remove JAVA_HOME configuration and clean up Java-related environment settings:

Remove User-Specific Configuration

Edit ~/.bashrc and remove any lines containing JAVA_HOME:

nano ~/.bashrc

Delete the export JAVA_HOME and associated PATH lines, then reload:

source ~/.bashrc

Remove System-Wide Configuration

Remove the profile.d script if you created one:

sudo rm /etc/profile.d/java-home.sh
sudo rm /etc/profile.d/java-path.sh

If you added JAVA_HOME to /etc/environment, edit and remove the line:

sudo nano /etc/environment

Remove Java Packages

To completely remove Java from the system:

sudo dnf remove java-*-openjdk*
sudo dnf autoremove

The first command removes all OpenJDK packages. The second command removes any orphaned dependencies that were installed alongside Java.

Frequently Asked Questions

Why does Java 21 become the default when both versions are installed?

Fedora’s alternatives system assigns higher priority numbers to version-pinned packages. Java 21 has priority 21000910 while java-latest has priority 1. In auto mode, alternatives selects the highest priority, making the pinned Java 21 the default. Use alternatives --set to override this if you prefer java-latest.

Should I use /etc/environment or /etc/profile.d/ for system-wide JAVA_HOME?

Use /etc/profile.d/ for system-wide JAVA_HOME configuration. Unlike /etc/environment which requires hardcoded paths, profile.d scripts can include shell logic to query the alternatives system and automatically track Java version changes. This provides system-wide configuration that adapts when administrators switch Java versions.

What is the difference between java-latest-openjdk and java-21-openjdk?

Both are LTS releases. The difference is version pinning: java-latest-openjdk is a rolling package that tracks the newest OpenJDK (currently 25, but will move to 26, 27, etc.). java-21-openjdk stays pinned to Java 21 forever. Choose java-21-openjdk when your project requires that specific version or needs predictability. Choose java-latest-openjdk to always have the newest Java features.

Do I need to update JAVA_HOME when switching Java versions?

It depends on your configuration method. If you use the dynamic JAVA_HOME methods that query the alternatives system, JAVA_HOME updates automatically in new terminal sessions. If you use static paths in .bashrc or /etc/environment, you must manually update the path after switching Java versions.

Conclusion

Configuring JAVA_HOME on Fedora ensures that build tools like Maven and Gradle, application servers like WildFly and Tomcat, and IDEs such as Eclipse and IntelliJ IDEA can locate your Java installation automatically. The recommended approach for most users is the system-wide dynamic configuration via /etc/profile.d/, which automatically tracks whichever Java version the alternatives system currently selects. This eliminates manual configuration updates when switching between different Java versions. For users requiring a specific Java version regardless of alternatives selection, the static configuration methods provide predictable behavior at the cost of manual updates when changing versions.

Leave a Comment

Let us know you are human: