How To Install Java 21 LTS (OpenJDK 21) on FreeBSD 14

Java has become one of the prominent programming languages in software application development. To be able to write, compile and run java code you will need a runtime environment to provide the necessary tools. With continuous gradual growth, Java 21 is currently the latest long-term release under the extensive collaboration of Oracle and other members of the worldwide Java developer community via OpenJDK Community and Java Community Process (JPC).

OpenJDK is an open-source implementation of Java Standard Edition (Java SE) and Java Development Kit (JDK). It comes with components such as a virtual machine, Java Class Library, and Java Compiler (javac). In this article, am going to take you through how to install OpenJDK 21 on FreeBSD 14.

Features of OpenJDK 21

  • String Templates – String templates complement Java’s existing string literals and text blocks by coupling literal text with embedded expressions and template processors to produce specialized results.
  • Sequenced Collections – Each such collection has a well-defined first element, second element, and so forth, up to the last element. It also provides uniform APIs for accessing its first and last elements, and for processing its elements in reverse order.
  • Record Patterns – Record patterns and type patterns can be nested to enable a powerful, declarative, and composable form of data navigation and processing.
  • Foreign Function & Memory API – By efficiently invoking foreign functions (i.e., code outside the JVM), and by safely accessing foreign memory (i.e., memory not managed by the JVM), the API enables Java programs to call native libraries and process native data without the brittleness and danger of JNI.
  • Virtual Threads – Virtual threads are lightweight threads that dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications.
  • Unnamed Patterns and Variables – Enhance the Java language with unnamed patterns, which match a record component without stating the component’s name or type, and unnamed variables, which can be initialized but not used.
  • Unnamed Classes and Instance Main Methods – Far from using a separate dialect of Java, students can write streamlined declarations for single-class programs and then seamlessly expand their programs to use more advanced features as their skills grow.
  • Key Encapsulation Mechanism API – Introduce an API for key encapsulation mechanisms (KEMs), an encryption technique for securing symmetric keys using public key cryptography.
  • e.t.c

Installation of OpenJDK 21 LTS in FreeBSD 14

In this part, we are going to cover how to install OpenJDK in FreeBSD 14 using the pkg command as software is readily available in FreeBSD repositories.

Step 1: Check available versions in upstream

Use below to check and confirm available versions of OpenJDK in the upstream.

$ pkg search ^openjdk
openjdk11-11.0.26+4.1_1        Java Development Kit 11
openjdk11-jre-11.0.26+4.1_1    Java Runtime Environment 11
openjdk17-17.0.14+7.1          Java Development Kit 17
openjdk17-jre-17.0.14+7.1      Java Runtime Environment 17
openjdk18-18.0.2+9.1_2         Java Development Kit 18
openjdk19-19.0.2+7.1_1         Java Development Kit 19
openjdk20-20.0.2+9.1           Java Development Kit 20
openjdk21-21.0.6+7.1           Java Development Kit 21
openjdk22-22.0.2+9.1           Java Development Kit 22
openjdk23-23.0.2+7.1_1         Java Development Kit 23
openjdk24-24.0.0+36.1          Java Development Kit 24
openjdk8-8.442.06.1_1          Java Development Kit 8
openjdk8-jre-8.442.06.1_1      Java Runtime Environment 8

Step 2: Install OpenJDK 21 in FreeBSD 14

From the above output, use the pkg command to specify version 21 of OpenJDK you want to install.

$ pkg install openjdk21
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 45 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
	alsa-lib: 1.2.13
	brotli: 1.1.0,1
	dejavu: 2.37_3
	encodings: 1.1.0,1
	expat: 2.7.0
	font-bh-ttf: 1.0.3_5
	font-misc-ethiopic: 1.0.4
	font-misc-meltho: 1.0.3_5
	fontconfig: 2.15.0_3,1
	freetype2: 2.13.3
	giflib: 5.2.2
	glib: 2.80.5_1,2
	graphite2: 1.3.14
	harfbuzz: 10.3.0
	javavmwrapper: 2.7.10
	jbigkit: 2.1_3
	jpeg-turbo: 3.1.0
	lcms2: 2.17
	lerc: 4.0.0
	libICE: 1.1.1,1
	libSM: 1.2.6,1
	libX11: 1.8.12,1
	libXau: 1.0.11
	libXdmcp: 1.1.5
	libXext: 1.3.6,1
	libXfixes: 6.0.1
	libXi: 1.8.1,1
	libXrandr: 1.5.4
	libXrender: 0.9.12
	libXt: 1.3.0,1
	libXtst: 1.2.4
	libdeflate: 1.22
	libfontenc: 1.1.8
	libiconv: 1.17_1
	liblz4: 1.10.0,1
	libxcb: 1.17.0
	mkfontscale: 1.2.3
	openjdk21: 21.0.6+7.1
	pcre2: 10.45
	png: 1.6.47
	py311-packaging: 24.2
	tiff: 4.7.0
	xorg-fonts-truetype: 7.7_1
	xorgproto: 2024.1
	zstd: 1.5.7

Number of packages to be installed: 45

The process will require 444 MiB more space.
192 MiB to be downloaded.

Proceed with this action? [y/N]: y

Installation complete:

Message from openjdk21-21.0.6+7.1:

--
This OpenJDK implementation may require fdescfs(5) mounted on /dev/fd
and procfs(5) mounted on /proc for some applications.

If you have not done it yet, please do the following:

	mount -t fdescfs fdesc /dev/fd
	mount -t procfs proc /proc

To make it permanent, you need the following lines in /etc/fstab:

	fdesc	/dev/fd		fdescfs		rw	0	0
	proc	/proc		procfs		rw	0	0

After performing the above operations now you should have Java fully installed and ready for use. Check and confirm Java installation in your FreeBSD system.

$ java -version
openjdk version "21.0.6" 2025-01-21
OpenJDK Runtime Environment (build 21.0.6+7-1)
OpenJDK 64-Bit Server VM (build 21.0.6+7-1, mixed mode, sharing)

Step 3: Set JAVA_HOME Environment in FreeBSD 14.

JAVA_HOME is an operating system environment variable for Java Development Kit (JDK) or Java Runtime Environment (JRE). This can be optionally set after the installation of JDK or JRE. JAVA_HOME variable points to a location where JDK or JRE is installed on a system. Java applications use this variable to find where the runtime is installed. In FreeBSD, you need to set the JAVA_HOME environment in /etc/csh.cshrc. Open either of the files and add the two lines below.

$ vim /etc/csh.cshrc
# $FreeBSD$
#
# System-wide .cshrc file for csh(1).
setenv JAVA_HOME /usr/local/openjdk21
setenv PATH $JAVA_HOME/bin:$PATH
#--

Make it the source path.

source /etc/csh.cshrc

Step 4: Switching Between Multiple Versions of Java in FreeBSD.

You may be running FreeBSD as your test Environment and you may need to apply with different versions of Java before taking it in a production environment. No need to keep on installing and uninstalling different versions. Instead, you can have multiple versions installed and switch to the one you want to use at a time. To achieve this, just edit the JAVA_HOME environment to the version you want.

For my case, am running both openjdk21 and openjdk17. To change to openjdk17, do as below;

$ nano /etc/csh.cshrc
# $FreeBSD$
#
# System-wide .cshrc file for csh(1).
setenv JAVA_HOME /usr/local/openjdk17
setenv PATH $JAVA_HOME/bin:$PATH
#--

Initialize the environment to apply changes. Thereafter confirm the active version of Java

##Reload your shell
$ source /etc/csh.cshrc
## Check Java Version
$ java -version
openjdk version "17.0.14" 2025-01-21
OpenJDK Runtime Environment (build 17.0.14+7-1)
OpenJDK 64-Bit Server VM (build 17.0.14+7-1, mixed mode, sharing)

Step 5: Run Java Test Application.

Create a hello world test file with the below content. 

$ vim HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello world!!");
    }
}

Compile the app using the javac command and run it.

$ javac HelloWorld.java
$ java HelloWorld
Hello world..!!!

Conclusion.

In this guide, we have learned how to install OpenJDK 21 in FreeBSD, one of the Linux distribution Operating systems. I believe you should be able to run multiple Java versions in FreeBSD to execute a Java application. Thank you for your support and continue browsing for more.

Similar articles:

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Unlock the Right Solutions with Confidence

At CloudSpinx, we don’t just offer services - we deliver clarity, direction, and results. Whether you're navigating cloud adoption, scaling infrastructure, or solving DevOps challenges, our seasoned experts help you make smart, strategic decisions with total confidence. Let us turn complexity into opportunity and bring your vision to life.

Leave a Comment

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

Related Post

In September 2023, Oracle announced the General Availability of Java 21, which comes bundled with tons of intriguing new features. […]

Upgrading to a newer version of FreeBSD can actually feel like a frightening and, in many ways, intimidating ordeal for […]

AlmaLinux is an open-source, community-driven, and cost-free distribution of the Linux operating system that is compatible with RHEL (Red Hat […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.