How to Install Java OpenJDK (17, 18, 19, 20, 21) on FreeBSD 14

In this article, I will show you how to install Java OpenJDK on a FreeBSD 14 server.

In detail, here’s what you’re going to learn:

  • Installing Java OpenJDK ( 21, 20, 19, 18, 17, or 11) on FreeBSD.
  • Managing multiple Java versions via javavmwrapper.
  • Setting up JAVA_HOME environment variable.
  • Setting up JAVA_OPTS for additional options in Java programs. For example, increasing max heap memory for java program.

Let’s get started.

Prerequisites

To complete this guide, confirm that you have the following:

Installing Java OpenJDK on FreeBSD

In this step, you will install Java OpenJDK on your FreeBSD machine.

There are multiple Java OpenJDK versions available on the FreeBSD repository, including OpenJDK 21, 20, 19, 18, 17, and 11. To choose between those, it is always recommended to use the LTS (Long Term Support) version. At this time, the Java OpenJDK 17 and 21 is the latest LTS version.

Complete the following actions to install Java OpenJDK on FreeBSD:

1. First, update your FreeBSD package index by executing the pkg command below.

pkg update

2. Now run the following command to install Java OpenJDK to your FreeBSD system. Be sure to select one or multiple OpenJDK versions in the following:

# installing Java OpenJDK 17
pkg install openjdk17

# installing Java OpenJDK 21
pkg install openjdk21

# installing Java OpenJDK 20
pkg install openjdk20

# installing Java OpenJDK 19
pkg install openjdk19

# installing Java OpenJDK 18
pkg install openjdk18

# installing Java OpenJDK 11
pkg install openjdk11

Type y to proceed with the installation.

Installing Java OpenJDK on FreeBSD
Installing Java OpenJDK on FreeBSD

3. After the installation is complete, run the java command below to verify your JDK (Java Development Kit) version.

# java command is part of JDK
java --version

Here’s the Java OpenJDK 17 that is installed.

Checking Java JDK version
Checking Java JDK version

4. Lastly, you can also check some of the JVM (Java Virtual Machine) tools version using the command below.

# javac and jar is part of JRE
javac --version
jar --version

You can see javac and jar version 17 are installed.

Checking Java JRE version
Checking Java JRE version

Setting up JAVA_HOME and default Java version

On FreeBSD, the Java OpenJDK configuration is managed by the javavmwrapper package, which is automatically installed when installing OpenJDK.

Now you will set up the JAVA_HOME environment variable and default Java version using the javavmwrapper utility. Here’s how you achieved that:

1. Run the command below to check the file /usr/local/etc/javavms and verify the list of installed Java versions on your FreeBSD system.

cat /usr/local/etc/javavms

In this example, I have 2 OpenJDK versions installed:

  • Java OpenJDK 21 installed in the /usr/local/openjdk21 directory.
  • Java OpenJDK 17 installed in the /usr/local/openjdk17 directory.
Checking installed Java versions and it's directory
Checking installed Java versions and it’s directory

2. Open the file /usr/local/etc/javavm_opts.conf using vim. This file is part of the javavmwrapper for managing Java installation, including the JAVA_HOME environment variable and multiple Java versions.

vim /usr/local/etc/javavm_opts.conf

Add the following configuration to set up the default Java version and JAVA_HOME environment variable to OpenJDK 17.

JAVA_HOME="/usr/local/openjdk17"

Or if you want to use OpenJDK 21, insert the configuration below.

JAVA_HOME="/usr/local/openjdk21"

Save and exit the file when finished.

3. Now run the command below to verify the JAVA_HOME environment variable on your FreeBSD server.

JAVAVM_DRYRUN=yes java

You will see below the JAVA_HOME environment variable is pointed to the OpenJDK 17 installation directory /usr/local/openjdk17.

Setting up JAVA_HOME via javavmwrapper on FreeBSD
Setting up JAVA_HOME via javavmwrapper on FreeBSD

4. Or run the java command below to verify the JAVA_HOME variable via Java advanced settings.

java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'

As seen below the java.home directory is pointed to the OpenJDK 17 directory /usr/local/openjdk17.

Checking JAVA_HOME via Java advanced settings
Checking JAVA_HOME via Java advanced settings

5. Lastly, run the following command to verify the default JDK and JVM versions on your FreeBSD server.

# check java JDK version
java --version

# check java JRE version
javac --version
jar --version

If everything goes well, you should get the OpenJDK 17 as the default Java JDK and JVM on your system.

Checking Java JDK and JRE version
Checking Java JDK and JRE version

Setting Up JAVA_OPTS via javavmwrapper

Another feature of javavmwrapper on FreeBSD is the ability to configure additional JAVA_OPTS for specific Java programs. For example, adding JAVA_OPTS for increasing max heap memory.

Here’s how to add JAVA_OPTS via javavmwrapper on FreeBSD:

1. Run the vim editor to open the file /usr/local/etc/javavm_opts.conf.

vim /usr/local/etc/javavm_opts.conf

Insert the following option to increase the default max heap memory for the java command to 512 MB and add new options -Xlint:rawtypes,unchecked for the javac program.

The JAVAVM_OPTS_java-program option allows you to configure additional JAVA_OPTS for specific Java programs. With the following example, you will add JAVA_OPTS for java and javac programs.

# JAVA_OPTS for java command - JDK Tool
JAVAVM_OPTS_java="-Xmx512m -Xms512m"

# JAVA_OPTS for javac command - JVM Tool
JAVAVM_OPTS_javac="-Xlint:rawtypes,unchecked"

Save and exit the file when you’re done.

2. Now run the command below to verify your JAVA_OPTS configuration. You can see the

JAVAVM_DRYRUN=yes java
JAVAVM_DRYRUN=yes javac

You can see below additional JAVA_OPTS for the java and javac programs are loaded.

Setting up JAVA_OPTS on FreeBSD via javavmwrapper
Setting up JAVA_OPTS on FreeBSD via javavmwrapper

3. Lastly, run the command below to verify the max heap memory for the java program.

java -XshowSettings -version
java -XshowSettings:vm -version

If successful, you will get the max heap memory for java is configured to 512M like the following:

Checking Java max heap memory based on javavmwrapper JAVA_OPTS
Checking Java max heap memory based on javavmwrapper JAVA_OPTS

Creating Hello World java application

With everything configured, you’re ready to develop and install Java applications. In this case, let’s test your Java OpenJDK installation by creating a simple Hello World application in Java. This will ensure both Java JDK and JRE are working on your system.

Follow these to create a Hello World application in Java:

1. Create a new Java application HelloJava.java using your preferred text editor and input the following Java code. This program will print you a message Hello Java - First Program.

class HelloJava
{
    public static void main(String args[])
    {
        System.out.println("Hello Java - First Program.");
    }
}

2. Now run the javac command below to compile the HelloJava.java program. Once compiled, you will see your file HelloJava.class.

# compiling Java code HellowJava.java
javac HelloJava.java

# verify file HelloJava.class
ls - HelloJava.class

3. Execute the HelloJava application using the java command below.

java HelloJava

The expected output will be the message Hello Java - First Program..

Creating, compiling, and running Java Hello World program
Creating, compiling, and running Java Hello World program

Uninstalling Java OpenJDK

If you need to remove Java OpenJDK from your system, follow these steps:

1. To delete/uninstall Java OpenJDK from your FreeBSD system, use the following pkg delete command.

pkg delete openjdk17 openjdk21

2. Once the process is complete, execute the following command to remove orphaned or unused packages from your system.

pkg autoremove

Conclusion

Congratulations! You have successfully installed Java OpenJDK on the FreeBSD 14 server. You’ve also learned how to use the javavmwrapper utility for managing multiple OpenJDK versions, setting up the JAVA_HOME environment variable, and adding options JAVA_OPTS to specific Java programs.

Last but not least, you’ve also created a simple Java Hello World application for testing the Java JDK and JRE installation.

Now you’re ready to develop or install Java-based applications on FreeBSD.

System administrator and devops enthusiast, leveraging over 10+ years of Linux expertise to optimize operations. Proficient in FreeBSD, VMWare, KVM, Proxmox, PfSense, Ansible, Docker, and Kubernetes.

Read Also: