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
|Category||Requirements, Conventions or Software Version Used|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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.
- Open a command line terminal and execute the following command to choose a different Java version to work with:
$ sudo update-alternatives --config java
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.
- 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.
- 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
Of course, you could also manually install Java from the official website.
- Once you have identified the version of Java that you want to install, use the
aptpackage manager to get it installed on the Raspberry Pi:
$ sudo apt install openjdk-11-jdk
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.
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.