How to Install OpenJDK 17 on Arch Linux

OpenJDK 17, a long-term support (LTS) release from the OpenJDK community, is essential for developers and system administrators working with Arch Linux due to its noteworthy features and enhancements. This guide is designed to walk you through the steps to efficiently install OpenJDK 17 on Arch Linux.

Key Features of OpenJDK 17:

  • Strong Encapsulation of JDK Internals: OpenJDK 17 enhances security by tightly encapsulating JDK internals, preventing unauthorized access.
  • Deprecation of SecurityManager: Marked for future removal, the deprecation of SecurityManager signals a transition towards alternative security measures.
  • Pattern Matching for Switch (Preview): The release introduces improved pattern matching for the ‘switch’ statement and expression, simplifying coding processes.
  • Enhanced Pseudo-Random Number Generators (JEP 356): OpenJDK 17 adds new interfaces and implementations for random number generation, providing developers with more tools for creating diverse functionalities.
  • New macOS Rendering Pipeline (JEP 382): OpenJDK 17 introduces a rendering pipeline based on Apple’s Metal framework for developers working with macOS.
  • Foreign Function & Memory API (Incubator – JEP 389): This feature allows Java programs to interoperate seamlessly with code and data outside the Java heap, expanding the possibilities for application development.

Now, with some of the historical key points covered, let’s proceed with the guide on how to install OpenJDK 17 on Arch Linux.

Update Arch Linux Before OpenJDK 17 Installation

First, update your Arch Linux system. Because Arch Linux uses a rolling release model, frequently update it. Run the following command in your terminal:

pacman -Syyu

Install OpenJDK 17 on Arch Linux

Step 1: Identifying Available JDK 17 Packages

Initiate the installation process by identifying the available OpenJDK 17 packages within the Arch Linux default repository. Execute the command below to filter and list all relevant packages:

pacman -Ss openjdk | grep 17
Screenshot displaying a search for OpenJDK 17 packages within the terminal on Arch Linux.
An illustrative screenshot provides a view of Arch Linux’s terminal, showcasing how to search for OpenJDK 17 packages using the command line effectively.

This terminal output clearly indicates the available Java 17 packages for installation on Arch Linux. For your convenience, you may modify the search term ‘17’ in the command to locate different Java versions, such as ‘11’ or ‘21’.

Step 1: Install OpenJDK Java 17 development kit

With the available packages listed, proceed to install OpenJDK 17. The command below will install both the OpenJDK 17 and its documentation:

sudo pacman -S jdk17-openjdk openjdk17-doc

In situations where the source is required, utilize the following command for installation:

sudo pacman -S openjdk17-src

Step 2: Install OpenJDK Java 17 full runtime environment

To establish a complete Java 17 runtime environment, execute the command below:

sudo pacman -S jre17-openjdk

For a headless environment, the alternative command is as follows:

sudo pacman -S jre17-openjdk-headless

Step 3: Confirm OpenJDK 17 Installation on Arch Linux

After the successful installation of the packages, verification of the OpenJDK 17 installation is crucial. Run the command below to display the installed Java version:

java -version

The output should align with the expected version details, confirming the successful installation of OpenJDK 17 on your Arch Linux system.

Test OpenJDK 17 Installation with a Test File on Arch Linux

Creating a Sample Java File

To verify the successful installation and functionality of OpenJDK 17, let’s create a simple “Hello World” Java program. Open a text editor of your choice, and input the following Java code:

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

Save this file with a .java extension, for example, HelloWorld.java. This code defines a HelloWorld class with a main method that prints “Hello, World!” to the console when executed.

Compiling the Java File

Navigate to the directory containing your HelloWorld.java file using the terminal. Once there, compile the Java file using the javac command provided by OpenJDK 17:

javac HelloWorld.java

This command will generate a HelloWorld.class file, which is the bytecode version of your Java program.

Running the Compiled Java Program

With the HelloWorld.class file generated, you can now execute the program using the java command:

java HelloWorld

Executing the terminal will display “Hello, World!”. This output verifies the correct installation of OpenJDK 17 and its ability to compile and run Java programs on your Arch Linux system.

Troubleshooting

Ensure OpenJDK 17 is properly installed and the JAVA_HOME environment variable is set correctly if you encounter any issues or errors during this process but this should not occur if installed via Arch Linux’s repository.

Switch Alternative Java Versions on Arch Linux

This section will cover if you are working between Java versions and how to switch to alternative versions using your Arch Linux command terminal.

Understanding the archlinux-java Command

Arch Linux facilitates a seamless transition between various installed Java versions through the archlinux-java command. This command empowers users to designate a default Java environment within the package.

Listing Installed Java Environments

To commence, it’s imperative to ascertain the Java versions residing on your system. Launch a terminal and execute the following command to enumerate all installed Java environments, with the active one distinctly marked:

archlinux-java status

Configuring the Default Java Environment

Having identified the Java environments, select and set your preferred version as the default. This is achieved by deploying the archlinux-java set command, appended by the name of the desired Java environment. For instance, to establish OpenJDK 17 as the default, input:

sudo archlinux-java set java-17-openjdk

It’s crucial to substitute java-17-openjdk with the precise name of the Java environment intended for default setting.

Verifying the Active Java Version

Post-switch, verifying that the system acknowledges and utilizes the newly set Java version is prudent. Execute the command below to affirm that the active Java version aligns with your selection:

java --version

Addressing Potential Issues Switching JDK Versions on Arch Linux

Should there be discrepancies, or if the java -version command output doesn’t mirror the anticipated Java version, meticulously review and ensure the accurate Java environment name was used during the archlinux-java set command execution.

Conclusion

This guide walked you through the straightforward process of installing OpenJDK 17 on Arch Linux. This installation allows developers and system administrators to leverage advanced features and enhancements for efficient application development. The streamlined installation process provides users a hassle-free experience and immediate access to OpenJDK 17’s latest improvements and updates. With OpenJDK 17 now on your Arch Linux system, you can build and run a wide range of Java applications, taking advantage of this LTS release’s robust capabilities.

Leave a Comment