How to change Java Version on Raspberry Pi

As Java is always being updated and changing, Java developers sometimes need to work with multiple versions of the JDK (Java Development Kit). It is particularly useful when supporting legacy programs that have been coded in a previous version of Java. For those using a Raspberry Pi to develop or run Java applications, it is possible to have multiple versions of the JDK installed, and switch among them as needed to accommodate older and newer Java programs. In this tutorial, you will see how to change the Java version on a Raspberry Pi.

In this tutorial you will learn:

  • How to change Java Version on Raspberry Pi
How to change Java Version on Raspberry Pi
How to change Java Version on Raspberry Pi
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

How to change Java Version on Raspberry Pi step by step instructions




Follow the steps below to change the default Java version on your Raspberry Pi system. You can just as easily switch back to another version, or even go back and forth between versions depending on which applications you need to work with at any given time.

  1. Open a command line terminal and execute the following command to choose a different Java version to work with:
    $ sudo update-alternatives --config java
    
    Selecting the default Java version to use with our Raspberry Pi
    Selecting the default Java version to use with our Raspberry Pi

    In the screenshot above, we are given two choices, which are Java version 11 and version 17. We can select the number that corresponds to our desired choice, and the changes will take effect immediately.

  2. Once the process is finished, you can verify the Java installation with the following command.
    $ java --version
    

    For further verification that you can compile and run Java programs, try compiling a Java Hello World program.

  3. In case you need to install some other version of Java first, you can check the ones that are available for installation from Debian’s software repository with the following command:
    $ sudo apt-cache search openjdk
    
    Various versions of Java available for installation
    Various versions of Java available for installation

    Of course, you could also manually install Java from the official website.

  4. Once you have identified the version of Java that you want to install, use the apt package manager to get it installed on the Raspberry Pi:
    $ sudo apt install openjdk-11-jdk
    


DID YOU KNOW?
For manual configuration when it comes to selecting your Raspberry Pi’s Java version, see our tutorial on How to locate and set JAVA home directory on Linux.

Closing Thoughts

In this tutorial, we saw how to change the Java version on a Raspberry Pi system. This included installing a different version of Java, and also configuring the default Java version that your Raspberry Pi should use. As seen here, it is quick and easy to change between Java programs, allowing users and developers to accommodate applications that only support one version or the other.



Comments and Discussions
Linux Forum