Java is one of the most popular programming languages for developing mobile and desktop applications. It provides a run-time environment to run several Java applications, including Tomcat, Jetty, Cassandra, Glassfish, and Jenkins. It is cross-platform and can run on Windows, Linux, and macOS.

In this post, we will show you how to install Java on Arch Linux.

Step 1 – Configure Repository

By default, the default repository is outdated in Arch Linux, so you will need to modify the default mirror list. You can do it by editing the mirrorlist configuration file:

nano /etc/pacman.d/mirrorlist

Remove all lines and add the following lines:

## Score: 0.7, United States
Server = http://mirror.us.leaseweb.net/archlinux/$repo/os/$arch
## Score: 0.8, United States
Server = http://lug.mtu.edu/archlinux/$repo/os/$arch
Server = http://mirror.nl.leaseweb.net/archlinux/$repo/os/$arch
## Score: 0.9, United Kingdom
Server = http://mirror.bytemark.co.uk/archlinux/$repo/os/$arch
## Score: 1.5, United Kingdom
Server = http://mirrors.manchester.m247.com/arch-linux/$repo/os/$arch
Server = http://archlinux.dcc.fc.up.pt/$repo/os/$arch
## Score: 6.6, United States
Server = http://mirror.cs.pitt.edu/archlinux/$repo/os/$arch
## Score: 6.7, United States
Server = http://mirrors.acm.wpi.edu/archlinux/$repo/os/$arch
## Score: 6.8, United States
Server = http://ftp.osuosl.org/pub/archlinux/$repo/os/$arch
## Score: 7.1, India
Server = http://mirror.cse.iitk.ac.in/archlinux/$repo/os/$arch
## Score: 10.1, United States
Server = http://mirrors.xmission.com/archlinux/$repo/os/$arch

Save and close the file, then update all the package indexes with the following command:

pacman -Syu

Step 2 – Install Java JRE and JDK

By default, Java JRE and JDK are available in the Arch Linux default repository.

First, search for all available Java JRE versions using the following command:

pacman -sS java | grep jre

You will get the following versions:

extra/jre-openjdk 18.0.2.1.u0-1 [installed]
extra/jre-openjdk-headless 18.0.2.1.u0-1 [installed]
extra/jre11-openjdk 11.0.16.1.u1-2
extra/jre11-openjdk-headless 11.0.16.1.u1-2
extra/jre17-openjdk 17.0.4.1.u1-2
extra/jre17-openjdk-headless 17.0.4.1.u1-2
extra/jre8-openjdk 8.345.u01-1
extra/jre8-openjdk-headless 8.345.u01-1

From the above list, install Java JRE 11 with the following command:

pacman -S jre11-openjdk

You can now verify the Java version using the following command:

java --version

You should see the following output:

openjdk 11.0.16.1 2022-08-12
OpenJDK Runtime Environment (build 11.0.16.1+1)
OpenJDK 64-Bit Server VM (build 11.0.16.1+1, mixed mode)

Next, search for all available Java JDK versions using the following command:

pacman -sS java | grep jdk

You will get the following list of versions:

extra/jdk-openjdk 18.0.2.1.u0-1
extra/jdk11-openjdk 11.0.16.1.u1-2
extra/jdk17-openjdk 17.0.4.1.u1-2
extra/jdk8-openjdk 8.345.u01-1
extra/jre-openjdk 18.0.2.1.u0-1
extra/jre-openjdk-headless 18.0.2.1.u0-1 [installed]
extra/jre11-openjdk 11.0.16.1.u1-2 [installed]
extra/jre11-openjdk-headless 11.0.16.1.u1-2 [installed]
extra/jre17-openjdk 17.0.4.1.u1-2
extra/jre17-openjdk-headless 17.0.4.1.u1-2
extra/jre8-openjdk 8.345.u01-1
extra/jre8-openjdk-headless 8.345.u01-1
extra/openjdk-doc 18.0.2.1.u0-1
extra/openjdk-src 18.0.2.1.u0-1
extra/openjdk11-doc 11.0.16.1.u1-2
extra/openjdk11-src 11.0.16.1.u1-2
extra/openjdk17-doc 17.0.4.1.u1-2
extra/openjdk17-src 17.0.4.1.u1-2
extra/openjdk8-doc 8.345.u01-1
extra/openjdk8-src 8.345.u01-1

Now, run the following command to install Java JDK 17:

pacman -S jdk17-openjdk

Step 3 – Set Default Java Versions

At this point, both Java versions are installed on your system. You can list all installed Java versions using the following command:

archlinux-java status

You will get the following output:

Available Java environments:
  java-11-openjdk (default)
  java-17-openjdk

Next, set Java JDK 17 as the default Java version using the following command:

archlinux-java set java-17-openjdk

Now, verify the Java versions using the following command:

java --version

You will get the following output:

openjdk 17.0.4.1 2022-08-12
OpenJDK Runtime Environment (build 17.0.4.1+1)
OpenJDK 64-Bit Server VM (build 17.0.4.1+1, mixed mode)

Step 4 – Install Oracle Java

When writing this article, the latest available Java version is Oracle Java 19. You can download it with the following command:

wget https://download.oracle.com/java/19/latest/jdk-19_linux-x64_bin.tar.gz

Once the download is completed, extract the downloaded file with the following command:

tar xzf jdk-19_linux-x64_bin.tar.gz -C /opt

Next, create an environment variable for Java:

nano /etc/profile.d/jdk19.sh

Add the following lines:

export JAVA_HOME="/opt/jdk-19/"
export PATH="$PATH:${JAVA_HOME}/bin"

Save and close the file, then load the environment variable with the following command:

source /etc/profile.d/jdk19.sh

Next, verify the Java home variable with the following command:

echo $JAVA_HOME

You will get the following output:

/opt/jdk-19/

You can now confirm the Oracle Java version using the following command:

/opt/jdk-19/bin/java --version

You should get the following output:

java 19 2022-09-20
Java(TM) SE Runtime Environment (build 19+36-2238)
Java HotSpot(TM) 64-Bit Server VM (build 19+36-2238, mixed mode, sharing)

Conclusion

In this post, we explained how to install Java JRE, Java JDK, and Oracle Java on Arch Linux. We also explained how to switch between the Java versions. You can now start developing your Java application using your preferred Java versions. Try Java on dedicated hosting from Atlantic.Net!