How to install Java 17 in FreeBSD 13

In this guide we are going to explore how to install Java Runtime Environment (JRE) and the Java Developer Kit (JDK) in FreeBSD 13 system.

Java and the JVM (Java’s virtual machine) are required for many kinds of software, including TomcatJettyGlassfishCassandra and Jenkins.

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. 

Also Check:

# Prerequisites

  • Up to date FreeBSD 13 system
  • Root access to the server or user with sudo access
  • Internet access from the server

# Table of content

  1. Ensuring that the server is up to date
  2. Installing java
  3. Testing the installation
  4. Toggling multiple Java versions
  5. Configuring the Environmental Variable

# Ensuring that the server is up to date

Before proceeding, let us ensure that our server is up to date and all the packages are the latest version. Use these commands to achieve this:

pkg update
pkg upgrade

If there are packages to upgrade, the above comand may take a couple of minutes.

Let us also install some common packages that we might need. I use vim text editor, and wget to download packages please make sure they are installed using this command:

pkg install -y vim

# Installing Java

Use the following command to check whether java is installed in your sytem.

# java -version
java: Command not found.

If you see<meta charset="utf-8">java: Command not found then it means that java is not installed. If java is installed, that command will print out the java version.

The OpenJDK 17 is available in the default FreeBSD 13 repos. Search using this command:

# pkg search openjdk
bootstrap-openjdk11-11.0.5.10.1 Java Development Kit 11
bootstrap-openjdk6-r450701     Java Development Kit 6
bootstrap-openjdk8-r450802_2   Java Development Kit 8
openjdk11-11.0.12+7.1          Java Development Kit 11
openjdk11-jre-11.0.12+7.1      Java Runtime Environment 11
openjdk12-12.0.2+10.4_3        Java Development Kit 12
openjdk13-13.0.8+5.1           Java Development Kit 13
openjdk14-14.0.2+12.1_2        Java Development Kit 14
openjdk15-15.0.4+5.1           Java Development Kit 15
openjdk16-16.0.2+7.1           Java Development Kit 16
openjdk17-17+35.1              Java Development Kit 17
openjdk8-8.302.08.1_2          Java Development Kit 8
openjdk8-jre-8.302.08.1_2      Java Runtime Environment 8
rxtx-openjdk8-2.2p2_4          Native interface to serial ports in Java

The package is available as openjdk17. Check info using this command:

# pkg search -o openjdk17
java/openjdk17                 Java Development Kit 17

Install the package using this command:

pkg install openjdk17

Now confirm the installed java version:

# java -version
openjdk version "17" 2021-09-14
OpenJDK Runtime Environment (build 17+35-1)
OpenJDK 64-Bit Server VM (build 17+35-1, mixed mode, sharing)

To verify if the JDK is installed properly, we will check the version of javac, which is the Java compiler.

# javac -version
javac 17

# Testing the installation

Let us test that Java 17 is installed properly in this step by creating a simple script that prints out Java is installed properly.

Create the directory and switch to it

mkdir simple-app
cd simple-app

I use vim to create and edit text files in my system. Use the text editor of your choice in this step. I will create a file Simple.java using this command:

vim Simple.java

Add these content to the file

public class Simple{
   public static void main(String[] args) {
      System.out.println("Citizix - Java is installed properly");
   }
}

Now that we have created our program, we need to compile the Java source code into bytecode (class file) using the javac compiler.

javac Simple.java

You will get a new file called Simple.class, which is the compiled Java class file.

$ ls
Simple.class  Simple.java

The command above will only compile the Java source code into bytecode. In order to run the program, we run the java command with the name of our class file as an argument.

java Simple

If everything works well, you’ll see a message “Citizix - Java is installed properly” on the screen.

$ java Simple
Citizix - Java is installed properly

# Conclusion

In this tutorial, you have learned how to install Java 17 on a FreeBSD system. If you need further information about the Java language, please refer to its official documentation.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy