Arch Linux is an independently developed Linux distribution that is lightweight and simple to use. It aims to keep its use as simple as possible with official packages optimized for the x86-64 architecture. Its main principles include Simplicity, versatility, modernity, pragmatism, and user centrality. It is developed with the user in mind that is feeling the needs of the ones who are contributing to it other than trying to look appealing to as many users as possible. Compared to other distributions Arch has some unique features like vanilla packaging where software libraries together with their header files. The arch repository has a massive amount of software accessibility and has an efficient package manager called Pacman. Arch is a rolling release distribution which means you do not have to install a newer version of the distribution upgrade.
Java is a popular programming language used widely for coding web applications, Games, mobile apps, and enterprise software to big data applications and server-side technologies. Java is open-source and free and its versatility enables a user to build localized and distributed software. It is also a computing platform that provides a reliable platform upon which many services and applications are built with its object-oriented, and network-centric language. Java is easy to learn and use with huge community support.
Java offers various high-quality development tools to support automated editing testing and debugging which saves time and is cost-efficient. It is multi-platform and can run independently on any platform including Windows, Linux, iOS, and Android. It is fast, powerful, and secure and has been used to develop major applications that include Oracle Applications, Gmail, Netbeans, Eclipse, and NASA Wind.
Java 17 includes the following features;
- Restore Always-Strict Floating-Point Semantics rather than having subtly different default floating-point semantics. This eases the development of numerically-sensitive libraries.
- Enhanced Pseudo-Random Number Generators provide new interface types to make it easier to use various PRNG algorithms interchangeably in applications and remove code duplication.
- Pattern Matching for switch ensures all existing switch expressions and statements continue to compile with no changes and execute with identical semantics.
- Deprecate the Applet API for Removal as all web-browser vendors have either removed support for Java browser plug-ins or announced plans to do so.
- Strongly Encapsulate JDK Internals which continues to improve the security and maintainability of the JDK.
Java 21 includes the following features;
- Record Patterns: Record patterns were included in Java 19 and Java 20 as preview features but are now included in Java 21. It extends the existing pattern-matching feature to destructure the record class instances, which enables writing sophisticated data queries.
- Virtual Threads: Virtual threads are lightweight threads with the purpose of reducing the effort of developing high-concurrent applications.
- Sequenced Collections: Java 21 introduced three new interfaces to represent sequenced collections, sequenced sets, and sequenced maps.
- Key Encapsulation Mechanism API: Key encapsulation is a technique to secure symmetric keys using asymmetric keys or public key cryptography. Java 21 has introduced a new KEM API to enable applications to use KEM algorithms.
In this guide, We are going to install Java 21,17 on Arch Linux | Manjaro|EndeavourOS.
1. Install Java 21, 17, 11, 8 on Arch Linux | Manjaro|EndeavourOS
Update your system packages.
sudo pacman -Syu
Install Java Runtime Environment (JRE)
Install JRE needed for executing Java GUI programs.
## Java 8##
sudo pacman -S jre8-openjdk
## Java 11##
sudo pacman -S jre11-openjdk
## Java 17##
sudo pacman -S jre17-openjdk
## Java 21 ##
sudo pacman -S jre21-openjdk
Install Java Development Kit (JDK)
Install the JDK that is needed for Java development. It depends on the full JRE.
##Java 8##
sudo pacman -S jdk8-openjdk
##Java 11##
sudo pacman -S jdk11-openjdk
##Java 17##
sudo pacman -S jdk17-openjdk
## Java 21 ##
sudo pacman -S jdk21-openjdk
To verify that java has been installed, check for the Java version.
## Java 8 ##
$ java -version
openjdk version "1.8.0_432"
OpenJDK Runtime Environment (build 1.8.0_432-b05)
OpenJDK 64-Bit Server VM (build 25.432-b05, mixed mode)
## Java 11 ##
$ java -version
openjdk version "11.0.25" 2024-10-15
OpenJDK Runtime Environment (build 11.0.25+9)
OpenJDK 64-Bit Server VM (build 11.0.25+9, mixed mode)
## Java 17 ##
$ java -version
openjdk version "17.0.13" 2024-10-15
OpenJDK Runtime Environment (build 17.0.13+11)
OpenJDK 64-Bit Server VM (build 17.0.13+11, mixed mode, sharing)
## Java 21 ##
$ java -version
openjdk version "21.0.5" 2024-10-15
OpenJDK Runtime Environment (build 21.0.5+11)
OpenJDK 64-Bit Server VM (build 21.0.5+11, mixed mode, sharing)
2. Switching between Java Versions
You can use the archlinux-java command to provide such functionalities.
List available Java environments
To list compatible java environments installed, run the following command
$ archlinux-java status
Available Java environments:
java-11-openjdk
java-17-openjdk
java-21-openjdk (default)
java-8-openjdk
Change the default Java environment
To change the default Java Environment use the following command syntax.
archlinux-java set <JAVA_ENV_NAME>
So in my case, to set Java 21 as the default Java environment, I would run the following command.
sudo archlinux-java set java-21-openjdk
Then to check if it has been set, run the following command.
$ archlinux-java status
Available Java environments:
java-11-openjdk
java-17-openjdk
java-21-openjdk (default)
java-8-openjdk
Unsetting the default Java environment
To unset the default java environment, use the unset command. But if a java environment is set to default, it will be removed and no environment will be the default.
sudo archlinux-java unset
Then check for the available Java environment.
$ archlinux-java status
Available Java environments:
java-8-openjdk
java-11-openjdk
java-17-openjdk
java-21-openjdk
No Java environment set as default
Fixing the default Java environment
If an invalid Java environment link is set, use the following command to fix it.
sudo archlinux-java fix
This will:
$ archlinux-java status
Available Java environments:
java-11-openjdk
java-17-openjdk
java-21-openjdk (default)
java-8-openjdk
Launching an application with the non-default java version
To launch an application with the non-default java version, you can wrap your application in a small bash script to locally change the default PATH of java. For example, to launch with Java 17, I would run the following command.
#!/bin/sh
export PATH="/usr/lib/jvm/java-17-openjdk/bin/:$PATH"
exec /path/to/application "$@"
Create a Sample Application
Create a java program file using the following command.
nano hello.java
Append the following command, You can change how you want the output to read.
public class hello {
public static void main(String[] args) {
System.out.println("Hello from Techviewleo!");
}
}
Save and exit the file. Compile Java with the following command.
javac hello.java
Then run the program using the following command.
$ java hello
Hello from Techviewleo!
Wrap Up
Java programming language is widely used to develop applications for different platforms including Android applications, Desktop applications like Gmail, Server applications, and Scientific applications like NASW Wind. Java is very popular among developers as it provides inbuilt functions and libraries that enable developers not to write new functions from scratch. It also has active community support that helps developers with their challenges and the platform is maintained and updated regularly.
Check out more articles: