How to check Raspberry Pi Java versions

Java can be an important component of a Raspberry Pi system for users that need to run programs that have been compiled with Java, or for developers that wish to code applications in Java. Since Java is constantly being developed, new versions are released and we may find that our Raspberry Pi needs to update to the latest version available, or download an older version for increased stability and legacy features. In this situation, we can install multiple versions of Java and then switch between them as needed.

In this tutorial, you will see how to check the installed Java versions on a Raspberry Pi. You will also learn how to switch between Java versions and install various versions of Java, whether that be the JDK (Java Development Kit) or the JRE (Java Runtime Environment) – we will cover the differences between these two types of software packages below, and help you choose the right one. Let’s get started!

In this tutorial you will learn:

  • What is the difference between Java JDK and JRE?
  • How to check the installed versions of Java on Raspberry Pi
  • How to change the version of Java and Java compiler
  • How to search for different versions of Java
  • How to install different versions of Java on Raspberry Pi
How to check Raspberry Pi Java versions
How to check Raspberry Pi Java versions
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Raspberry Pi
Software Java
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Checking for Installed Java Versions




The first thing to do is check which versions of Java are already installed. It is typical to have more than one version available, or perhaps your system is missing the Java package entirely, or only has one verion installed. The commands below will reveal what we need to know.

To see which versions of Java are installed on your Raspberry Pi, execute the following command:

$ sudo update-alternatives --config java
Checking which versions of Java are installed on Raspberry Pi
Checking which versions of Java are installed on Raspberry Pi

In the screenshot above, we can see that Java 11 and Java 17 are the two versions currently installed on the Raspberry Pi. If we want to switch Java versions, we can enter the number of the row that corresponds to which version we would like to use.

To see which version of Java is currently in use, we can execute:

$ java --version
openjdk version "17.0.10" 2024-01-16
OpenJDK Runtime Environment (build 17.0.10+7-Debian-1deb11u1)
OpenJDK Server VM (build 17.0.10+7-Debian-1deb11u1, mixed mode, sharing)

This simply means that the java command is currently symbolically linked to the version listed in the output above. Running the previous update-alternatives is the best way to change the current version, which will update the symbolic links as necessary.

Changing Java Compiler Version

Those wishing to compile their programs with Java will need to also check the javac command in order to know which version of Java their programs are set to be compiled with.

To reveal available Java compiler versions, the following command can be run:

$ sudo update-alternatives --config javac

And to see which version of the Java compiler is currently set to be used:

$ javac -version
javac 17.0.10


Install Alternative Version of Java on Raspberry Pi

Now that we know which versions of Java are available on our Raspberry Pi, we can consider installing alternative or additional versions.

Installation via Package Manager

If you would like to install Java via the default package manager, then we can search for available packages with the following apt command:

$ sudo apt-cache search openjdk
Java versions that are available for installation on Raspberry Pi
Java versions that are available for installation on Raspberry Pi

In our case, the search reveals that there are two major versions available for install – Java version 11 and version 17. Based on your requirements, you will need to decide which version to install, along with what type of package – JDK, JRE, headless, etc.

WHAT TYPE OF JAVA PACKAGE SHOULD I INSTALL?
openjdk – The Java Development Kit is for programmers who want to write applications by coding in Java, and then compile their programs. The JDK package is necessary for this.
openjre – The Java Runtime Environment is for running Java programs. If you have a Java program that you want to run on the Raspberry Pi, you will need to install this package.
-headless – The headless packages are designed for users not running a GUI. If your Raspberry Pi is command line only, then you will want to install either the openjdk-headless or openjre-headless package.

By using the information above, you can determine which type of Java package is right for you. Let’s proceed by installing version 17 of the Open JDK.

$ sudo apt install openjdk-17-jdk

Installation via Official Website

  1. In addition to the Java versions maintained in the default software repository, we can also access the Java download page on the official website for more download options.
    Downloading Java from the Oracle website
    Downloading Java from the Oracle website

    Here you can select from more versions to download, but they will need to be manually installed. Be sure to select the ARM compressed archive, as other architectures will not work with the Raspberry Pi, of course.

  2. Decompress the tarball that you just downloaded:
    $ cd ~/Downloads
    $ tar xzvf jdk-22*
    
  3. And then move the directory to wherever you would like the new Java version to be installed. The /opt direcory is usually a good choice:
    $ sudo mv ~/Downloads/jdk-22 /opt
    
  4. You will need to manually update the symlinks in order to run the java and javac commands under the new version you downloaded.
    $ sudo ln -vfns /opt/jdk-22/bin/java /usr/bin/java
    $ sudo ln -vfns /opt/jdk-22/bin/javac /usr/bin/javac
    



  5. Check the version currently in use to make sure it works as expected:
    $ java -version
    java version "22" 2024-03-19
    Java(TM) SE Runtime Environment (build 22+36-2370)
    Java HotSpot(TM) 64-Bit Server VM (build 22+36-2370, mixed mode, sharing)
    

    AND

    $ javac -version
    javac22
    
    Verifying the newly installed Java version on Raspberry Pi
    Verifying the newly installed Java version on Raspberry Pi

Closing Thoughts




In this tutorial, we saw how to check the installed version of Java on a Raspberry Pi system. We also learned how to switch between the installed versions, along with installing new versions both from the software repository with apt package manager, and the official Oracle website. This will allow Java developers and those wishing to run Java programs the ability to install and utilize the exact version of Java that they need for their projects.



Comments and Discussions
Linux Forum