How to Install Java 21

Understanding Java 21 and Its Significance

Java 21, released in September 2023, represents a significant milestone in the evolution of the Java Development Kit (JDK). As a Long-Term Support (LTS) release, it promises an extended period of stability and ongoing support, making it an attractive choice for developers and organizations seeking a robust and reliable platform for their applications. LTS releases are crucial for enterprise environments where stability, security patches, and backward compatibility are paramount. Java 21 builds upon the foundation laid by previous LTS versions, introducing a host of new features, performance enhancements, and crucial bug fixes that aim to streamline development, improve application efficiency, and unlock new possibilities.

The LTS designation means that Java 21 will receive regular updates, including security patches and performance improvements, for several years. This predictability is invaluable for businesses that rely on Java for critical applications, reducing the need for frequent and potentially disruptive upgrades. Developers can confidently build and deploy applications knowing that their chosen Java version will remain supported and secure for the foreseeable future. This commitment to long-term support is a testament to Oracle’s dedication to the Java ecosystem and its role in powering a vast array of software, from web applications and mobile apps to big data processing and embedded systems.

Java 21’s feature set reflects the ongoing efforts to modernize the language and its associated technologies. This includes enhancements to existing APIs, the introduction of new language constructs, and improvements to the Java Virtual Machine (JVM). These advancements are designed to address contemporary development challenges, such as improving concurrency, simplifying code, and enhancing performance. The Java platform’s adaptability and continuous innovation are key reasons for its enduring popularity and its widespread adoption across diverse industries. Whether you are a seasoned Java developer or new to the platform, understanding the benefits and installation process of Java 21 is essential for leveraging its full potential.

Prerequisites for Installation

Before embarking on the Java 21 installation process, ensuring that your system meets the necessary prerequisites is a critical step for a smooth and successful setup. These prerequisites generally involve checking your operating system compatibility and, in some cases, verifying if a compatible Java Runtime Environment (JRE) or an older JDK is already present. While Java 21 can be installed on most modern operating systems, it’s always advisable to consult the official Oracle documentation for the most up-to-date system requirements.

Operating System Compatibility

Java 21 is designed to run on a wide range of operating systems, including Windows, macOS, and various Linux distributions.

  • Windows: Support typically extends to Windows 10 and later versions, as well as Windows Server editions. Ensure you have administrative privileges to install software on your Windows machine.
  • macOS: Compatibility usually covers recent macOS versions. For macOS installations, you might need to grant specific permissions during the installation process.
  • Linux: Java 21 is compatible with numerous Linux distributions such as Ubuntu, Fedora, CentOS, and Debian. Installation methods may vary slightly depending on the distribution, often involving package managers or manual extraction of archives. It’s crucial to have the necessary permissions to install software system-wide.

Hardware and Software Requirements

Beyond the operating system, certain hardware and software configurations are recommended or required for optimal Java 21 performance.

  • RAM: While Java can run with minimal RAM, having at least 4 GB of RAM is recommended for smoother development and execution of Java applications, especially for larger projects or when running multiple applications concurrently.
  • Disk Space: The JDK installation requires a certain amount of disk space, typically a few hundred megabytes. It’s good practice to have ample free space available on your hard drive.
  • Internet Connection: A stable internet connection is necessary to download the Java 21 installer or archive files from the official Oracle website or other trusted sources.
  • Administrative Privileges: For a system-wide installation, you will need administrative rights on your computer. This allows the installer to place files in system directories and configure necessary environment variables.

Verifying Existing Java Installations

It’s beneficial to check if you have any existing Java installations on your system. While multiple Java versions can coexist, conflicts can arise if not managed properly. To check for an existing Java installation on Windows, you can open the Command Prompt and type:

java -version

On macOS or Linux, you can open the Terminal and execute the same command. If Java is installed, this command will display the version information. If it’s not installed or you want to ensure you’re using Java 21 specifically, you’ll proceed with the installation. It’s also wise to understand how your system manages Java installations, particularly on Linux where alternatives might be used to switch between different Java versions.

Installing Java 21 on Windows

The installation of Java 21 on a Windows system is a straightforward process that typically involves downloading the installer from Oracle and following a series of on-screen prompts. Ensuring that you download the correct installer for your system architecture (32-bit or 64-bit) is crucial for optimal performance and compatibility. Most modern Windows systems are 64-bit.

Downloading the JDK

  1. Visit the Official Oracle JDK Download Page: Navigate to the official Oracle Java SE Downloads page. You will find links to download the latest JDK releases, including Java 21.
  2. Select the Correct Installer: Look for the “JDK Download” section for Java 21. You will see options for various operating systems. Choose the installer for “Windows.” It’s usually provided as an .exe file. Ensure you select the installer that matches your system’s architecture (x64 for 64-bit Windows).
  3. Download the File: Click the download link. You may be prompted to accept the Oracle license agreement. After accepting, the download of the .exe file will commence.

Running the Installer

  1. Execute the Installer: Once the download is complete, locate the downloaded .exe file and double-click it to launch the installation wizard.
  2. User Account Control (UAC): If prompted by User Account Control, click “Yes” to allow the installer to make changes to your device.
  3. Welcome Screen: The installer will present a welcome screen. Click “Next” to proceed.
  4. Custom Setup: The installer will typically offer a “Custom Setup” option. By default, this includes the JDK and the Java Runtime Environment (JRE). For most development purposes, it’s recommended to install both. You can choose the installation directory here, though the default location (usually C:Program FilesJavajdk-21) is generally suitable. Click “Next.”
  5. Installation Progress: The installer will now extract and install the Java Development Kit files. This process may take a few minutes.
  6. Completion: Upon successful installation, you will see a completion screen. Click “Close.”

Configuring Environment Variables (JAVA_HOME and PATH)

Setting up the JAVA_HOME and PATH environment variables is essential for your Windows system to locate and use the Java 21 installation effectively. This is particularly important for command-line tools and build systems.

  1. Locate the JDK Installation Directory: Navigate to the directory where you installed the JDK. For example, it might be C:Program FilesJavajdk-21. Copy this path.
  2. Open System Properties:
    • Right-click on “This PC” (or “Computer” on older Windows versions).
    • Select “Properties.”
    • Click on “Advanced system settings” on the left-hand pane.
  3. Environment Variables: In the “System Properties” window, click the “Environment Variables…” button.
  4. Set JAVA_HOME:
    • Under “System variables” (or “User variables” if you only want it for your user account), click “New…”
    • In the “Variable name” field, enter JAVA_HOME.
    • In the “Variable value” field, paste the path to your JDK installation directory (e.g., C:Program FilesJavajdk-21).
    • Click “OK.”
  5. Edit the PATH Variable:
    • In the “System variables” list, find the variable named Path and select it.
    • Click the “Edit…” button.
    • In the “Edit environment variable” window, click “New.”
    • Enter %JAVA_HOME%bin in the new line. This tells Windows to look for Java executables in the bin directory of your JAVA_HOME path.
    • Click “OK” on all open windows to save the changes.

Verifying the Installation

  1. Open Command Prompt: Open a new Command Prompt window (search for cmd in the Start menu). It’s important to open a new one after setting environment variables for the changes to take effect.
  2. Check Java Version: Type the following command and press Enter:
    bash
    java -version

    You should see output indicating that you are running “Java version 21”.
  3. Check Java Compiler Version: Type the following command and press Enter:
    bash
    javac -version

    This command checks the Java compiler (javac), which is part of the JDK. The output should also confirm Java version 21.

If both commands display the correct version, your Java 21 installation and environment variable configuration on Windows are successful.

Installing Java 21 on macOS

Installing Java 21 on macOS involves downloading the appropriate installer package and then configuring your system’s environment variables to point to the new installation. macOS offers a graphical installer that simplifies the process.

Downloading the JDK

  1. Visit the Official Oracle JDK Download Page: Go to the Oracle Java SE Downloads page.
  2. Select the Correct Installer: Locate the section for Java 21. Under the “macOS” tab, you will find different package formats. The most common and recommended is the .dmg (Disk Image) file. Choose the installer for your Mac’s architecture, typically “x64 DMG Archive” for Intel-based Macs or “AArch64 DMG Archive” for Apple Silicon (M1/M2/M3) Macs.
  3. Download the File: Click the download link. You may need to accept the Oracle license agreement. The .dmg file will begin downloading.

Running the Installer

  1. Mount the Disk Image: Once the .dmg file has finished downloading, double-click it. This will mount the disk image and open a Finder window containing the installer package.
  2. Run the Installer Package: Double-click the .pkg file (e.g., jdk-21_macos-aarch64_bin.pkg). This will launch the macOS installer.
  3. Introduction and License: The installer will guide you through the process. Review the introduction and accept the software license agreement.
  4. Installation Type: You will be prompted to choose an installation location. The default location is usually /Library/Java/JavaVirtualMachines/ and is recommended for most users. Click “Install.”
  5. Administrator Password: You will likely be asked to enter your macOS administrator password to authorize the installation.
  6. Installation Progress: The installer will copy the necessary files to your system. This process can take a few minutes.
  7. Summary: Upon completion, you will see a summary indicating that the installation was successful. You can then close the installer.

Configuring Environment Variables (JAVA_HOME and PATH)

On macOS, environment variables are typically managed through shell configuration files. The most common files are .zshrc (for Zsh, the default shell in recent macOS versions) or .bash_profile (for Bash).

  1. Locate the JDK Installation Directory: The installer usually places the JDK in a location like /Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home. You can confirm this by navigating to /Library/Java/JavaVirtualMachines/ in Finder or using the Terminal.

  2. Open Terminal: Launch the Terminal application (Applications > Utilities > Terminal).

  3. Edit the Shell Configuration File:

    • For Zsh: Type nano ~/.zshrc and press Enter.
    • For Bash: Type nano ~/.bash_profile and press Enter.
      This will open the specified file in the nano text editor.
  4. Add Environment Variables: Add the following lines to the end of the file, replacing /path/to/your/jdk-21 with the actual path to your Java 21 installation:

    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home
    export PATH=$PATH:$JAVA_HOME/bin
    
    • export JAVA_HOME=...: This sets the JAVA_HOME environment variable to the root directory of your Java 21 installation.
    • export PATH=$PATH:$JAVA_HOME/bin: This adds the bin directory of your Java installation to your system’s PATH, allowing you to run Java commands from anywhere in the Terminal.
  5. Save and Exit:

    • Press Ctrl + X to exit nano.
    • Press Y to confirm saving the changes.
    • Press Enter to confirm the filename.
  6. Apply Changes: To make the changes take effect immediately without restarting your Terminal, run one of the following commands:

    • For Zsh: source ~/.zshrc
    • For Bash: source ~/.bash_profile

Verifying the Installation

  1. Open Terminal: Ensure you have a Terminal window open after applying the changes or have opened a new one.
  2. Check Java Version: Type the following command and press Enter:
    bash
    java -version

    You should see output confirming that you are using “version 21”.
  3. Check Java Compiler Version: Type the following command and press Enter:
    bash
    javac -version

    This command verifies the Java compiler (javac). The output should also indicate version 21.

If both commands display the correct version, your Java 21 installation on macOS is complete and correctly configured.

Installing Java 21 on Linux

Installing Java 21 on Linux can be achieved through package managers (which is the recommended method for most distributions) or by manually downloading and extracting the binary archive. Using a package manager ensures that Java is integrated correctly with your system’s software management.

Using a Package Manager (Recommended)

Most popular Linux distributions provide OpenJDK or Oracle JDK packages in their repositories. The exact commands may vary slightly depending on your distribution.

For Debian/Ubuntu-based distributions (e.g., Ubuntu, Mint):

  1. Update Package List:
    bash
    sudo apt update
  2. Search for Available Java 21 Packages:
    bash
    apt search openjdk-21

    This will list available packages related to OpenJDK 21. You will typically look for a package like openjdk-21-jdk.
  3. Install the JDK:
    bash
    sudo apt install openjdk-21-jdk

    This command installs the Java Development Kit for version 21.

For Fedora/CentOS/RHEL-based distributions:

  1. Update Package List:
    bash
    sudo dnf update

    (Or sudo yum update for older versions of RHEL/CentOS).
  2. Search for Available Java 21 Packages:
    bash
    sudo dnf search java-17-openjdk # Often version 17 is the LTS, search for latest available.

    Linux distributions sometimes lag behind in offering the absolute latest LTS versions immediately via package managers. You might find OpenJDK 17 (the previous LTS) readily available. If OpenJDK 21 is not yet in the default repositories, you might need to use the manual method or add a third-party repository. Assuming java-21-openjdk-devel is available:
  3. Install the JDK:
    bash
    sudo dnf install java-21-openjdk-devel

Managing Multiple Java Versions (update-alternatives):

On many Linux systems, the update-alternatives command is used to manage multiple versions of commands, including java and javac. After installing Java 21, you might need to set it as the default.

  1. List Installed Java Versions:
    bash
    sudo update-alternatives --config java

    This command will show a list of installed Java executables and prompt you to select the default one. Choose the number corresponding to your Java 21 installation.
  2. Configure the Compiler: Do the same for the Java compiler:
    bash
    sudo update-alternatives --config javac

    Select the Java 21 compiler from the list.

Manual Installation (Binary Archive)

If your distribution does not have Java 21 in its repositories or you prefer a manual installation:

  1. Download the Binary Archive:

    • Go to the Oracle Java SE Downloads page.
    • Select Java 21.
    • Under the “Linux” tab, choose the appropriate archive for your system architecture: “x64 Compressed Archive” (.tar.gz).
    • Download the .tar.gz file.
  2. Extract the Archive:

    • Open your Terminal.
    • Navigate to the directory where you downloaded the file (e.g., ~/Downloads).
    • Create a directory to store your Java installations, for example, /opt/java:
      bash
      sudo mkdir -p /opt/java
    • Extract the downloaded archive into this directory:
      bash
      sudo tar -xvzf jdk-21.<version_details>.tar.gz -C /opt/java

      Replace jdk-21.<version_details>.tar.gz with the actual filename. This will create a directory like /opt/java/jdk-21.
  3. Configure Environment Variables:

    • Edit your shell configuration file (~/.bashrc for Bash, or ~/.zshrc for Zsh):
      bash
      nano ~/.bashrc # or nano ~/.zshrc
    • Add the following lines to the end of the file:
      bash
      export JAVA_HOME=/opt/java/jdk-21
      export PATH=$PATH:$JAVA_HOME/bin

      Adjust JAVA_HOME path if your extracted directory name is different.
    • Save and exit nano (Ctrl+X, Y, Enter).
    • Apply the changes:
      bash
      source ~/.bashrc # or source ~/.zshrc

Verifying the Installation

  1. Open Terminal: Ensure you have a Terminal window open after installation or configuration.
  2. Check Java Version: Type the following command and press Enter:
    bash
    java -version

    The output should confirm “version 21”.
  3. Check Java Compiler Version: Type the following command and press Enter:
    bash
    javac -version

    This should also display “version 21”.

Successful execution of these commands indicates that Java 21 has been correctly installed and configured on your Linux system.

Post-Installation and Verification

After successfully installing Java 21 on your chosen operating system, it’s crucial to perform a few post-installation checks and understand how to manage your Java environment. This ensures that the installation is recognized by your system and that you can effectively use it for development and running Java applications.

Verifying Installation Across Platforms

As detailed in the platform-specific installation sections, the universal method for verifying your Java 21 installation is to open a new terminal or command prompt window and execute:

java -version

and

javac -version

Both commands should output version 21 if the installation was successful and the environment variables were correctly set. If you receive an error message like “command not found,” it indicates that the PATH environment variable is not correctly configured, or the JDK was not installed in a location recognized by the system. Revisit the environment variable setup for your specific operating system.

Understanding the JDK vs. JRE

It’s important to distinguish between the Java Development Kit (JDK) and the Java Runtime Environment (JRE).

  • JDK: This is a complete development environment that includes tools necessary for writing, compiling, and debugging Java applications. It contains the JRE, plus development tools such as the javac compiler, javadoc for generating documentation, and various debugging utilities. When you install the JDK, you automatically get the JRE.
  • JRE: This is a subset of the JDK and contains only the necessary components to run Java applications. It includes the Java Virtual Machine (JVM) and the core Java libraries. If you only need to run Java applications and not develop them, you might only install the JRE. However, for most developers, installing the JDK is the standard practice.

When you install Java 21, you are typically installing the JDK, which includes the JRE. This provides you with the flexibility to both develop and run Java applications.

Setting the Default Java Version (Linux/macOS)

On Linux and macOS, update-alternatives (Linux) or manual configuration of shell profiles (macOS and Linux) is used to manage which Java version is considered “default.”

  • Linux (update-alternatives): As shown in the Linux installation section, sudo update-alternatives --config java and sudo update-alternatives --config javac allow you to select the active Java version from multiple installations.
  • macOS/Linux (Shell Profiles): If you manually installed Java or have multiple JDKs and the system doesn’t pick up the correct one, ensuring your JAVA_HOME and PATH variables in your ~/.zshrc, ~/.bashrc, or ~/.bash_profile are correctly pointing to the desired Java 21 installation is key. The order in which directories are listed in the PATH variable matters; the first matching executable found will be used.

Using the java and javac Commands

Once installed and configured, you can use the java command to run compiled Java applications ( .class files or JAR files) and the javac command to compile your Java source code (.java files) into bytecode.

Example Compilation:

Create a file named HelloWorld.java with the following content:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Java 21!");
    }
}

Save the file and then compile it using the javac command in your terminal or command prompt:

javac HelloWorld.java

This will create a HelloWorld.class file.

Example Execution:

Now, run the compiled class using the java command:

java HelloWorld

The output will be:

Hello, Java 21!

This simple process confirms that your Java 21 JDK is operational and ready for use.

Further Steps and Best Practices

  • IDE Integration: If you use an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or VS Code, you’ll need to configure your IDE to recognize your Java 21 JDK installation. Most IDEs have a setting where you can specify the JDK home directory. This allows the IDE to use the correct compiler, debugger, and other tools for your project.
  • Build Tools: For projects managed with build tools like Maven or Gradle, ensure that your project’s configuration (e.g., pom.xml for Maven, build.gradle for Gradle) specifies Java 21 as the target and source compatibility version. This ensures that your code is compiled and run using the features and behavior of Java 21.
  • Regular Updates: While Java 21 is an LTS release, it will receive updates. Stay informed about these updates by checking Oracle’s release notes and consider applying them to your system to benefit from security patches and performance improvements.
  • Understand New Features: Familiarize yourself with the new features introduced in Java 21, such as the preview of Virtual Threads (Project Loom), Pattern Matching for switch, and String Templates. These can significantly enhance your development experience and application performance.

By following these post-installation steps, you can ensure a robust and well-configured Java 21 environment, setting a solid foundation for your Java development endeavors.

Leave a Comment

Your email address will not be published. Required fields are marked *

FlyingMachineArena.org is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. Amazon, the Amazon logo, AmazonSupply, and the AmazonSupply logo are trademarks of Amazon.com, Inc. or its affiliates. As an Amazon Associate we earn affiliate commissions from qualifying purchases.
Scroll to Top