There are 2 versions of JDK. OpenJDK and Oracle JDK. OpenJDK is open source and does not contain any proprietary codes. Oracle JDK is closed source and contains proprietary codes from Oracle.
In this article, I am going to show you how to install Oracle JDK 12 and OpenJDK 12 on Arch Linux. So, let’s get started.
Installing Oracle JDK 12:
Oracle JDK 12 is not available in the official package repository of Arch Linux. But, you can easily download and install Oracle JDK 12 from the official website of Oracle and install it on Arch Linux.
First, visit https://www.oracle.com/technetwork/java/javase/downloads/index.html from your favorite web browser and click on the DOWNLOAD button for Java SE 12.0.1 or Java Platform (JDK) 12.
Now, click on Accept License Agreement to accept the Oracle Technology Network License Agreement for Oracle Java SE.
Now, click on the jdk-12.0.1_linux-x64_bin.tar.gz link as marked in the screenshot below.
Your browser should prompt you to save the file. Click on Save File and then click on OK.
Your browser should start downloading Oracle JDK tar.gz archive. It may take a while to complete.
Once the download is complete, navigate to the ~/Downloads directory with the following command:
As you can see, the Oracle JDK 12 tar.gz archive I just downloaded is here.
Now, extract the Oracle JDK 12 tar.gz archive in the /opt directory with the following command:
The JDK 12 tar.gz archive should be extracted.
As you can see, a new directory jdk-12.0.1/ is created in the /opt directory. Note the directory name.
Now, you have to create a new environment variable JAVA_HOME and add Oracle JDK 12 binaries to the PATH environment variable. To do that, run the following command:
| sudo tee /etc/profile.d/jdk12.sh
A new file /etc/profile.d/jdk12.sh should be created.
Now, restart your Arch Linux machine with the following command:
Once your computer boots, run the following command to check whether the JAVA_HOME and PATH environment variables are set correctly.
As you can see, the JAVA_HOME and PATH environment variables are correctly set.
Now, check whether Oracle JDK 12 is working with the following command:
As you can see, Oracle JDK 12 is working correctly.
Installing OpenJDK 12:
OpenJDK 12 is not available in the official package repository of Arch Linux. But, you can download it from the official website of OpenJDK and install it on Arch Linux.
First, visit the official website of OpenJDK 12 at https://jdk.java.net/12/. Once the page loads, click on the tar.gz click as marked in the screenshot below.
Your browser should prompt you to save the OpenJDK 12 tar.gz archive. Select Save File and click on OK.
Your browser should start downloading OpenJDK 12 tar.gz archive. It may take a while to complete.
Once the download is complete, navigate to the ~/Downloads directory with the following command:
As you can see, the OpenJDK 12 tar.gz archive is here.
Now, extract the OpenJDK 12 tar.gz archive in the /opt directory with the following command:
OpenJDK 12 tar.gz archive should be downloaded.
A new directory jdk-12.0.1/ should be created in the /opt directory.
Now, you have to create a new JAVA_HOME environment variable and update the PATH environment variable. To do that, run the following command:
| sudo tee /etc/profile.d/openjdk12.sh
A new file /etc/profile.d/openjdk12.sh should be created.
Now, reboot your computer with the following command:
Once your computer boots, run the following command to check whether the environment variables JAVA_HOME and PATH are correctly set.
Now, run the following command to check whether the OpenJDK 12 is working.
As you can see, OpenJDK 12 is working correctly.
Compiling a Simple Java Program with JDK 12:
In this section, I will show you how to compile and run a simple Java program with JDK 12.
I have written a simple Java program Welcome.java and saved it in the ~/codes directory.
The contents of the Welcome.java Java source file are as follows:
Now, to compile the Welcome.java source file, run the following command:
The Welcome.java Java source file should be compiled and a new file Welcome.class should be generated as you can see in the screenshot below. It’s called a Java class file. The Java source code is converted into Java bytecode and the converted bytecode is saved in the class file, which the JVM (Java Virtual Machine) can run.
Now, you can run the compiled Java class file as follows:
NOTE: When you want to run a Java class file, exclude the .class extension.
As you can see, the correct output is displayed.
So, that’s how you install Oracle JDK 12 and OpenJDK 12 on Arch Linux. Thanks for reading this article.