Search code examples
javaraspberry-pi32-bitjava-17

Upgrade Java 16 to Java 17 in 32bit Raspberry


Installing the latest version of Java is always a bit messy and wanted to see if I'm doing everything right. I currently have Java 16 in this path /usr/lib/jvm/adoptopenjdk-16-hotspot-armhf

I followed the following tutorial 2) Install OpenJDK 17 on Debian 10/9 and everything went OK.

My JAVA_HOME is correct and set to /opt/jdk17, but my java --version is still using Java 16.

java --version
openjdk 16.0.1 2021-04-20
OpenJDK Runtime Environment AdoptOpenJDK-16.0.1+9 (build 16.0.1+9)
OpenJDK Server VM AdoptOpenJDK-16.0.1+9 (build 16.0.1+9, mixed mode)

Other useful information

pi@raspberrypi:~ $ which java
/usr/bin/java

pi@raspberrypi:~ $ echo $PATH
/home/pi/.local/bin:/bin:/home/pi/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

My PATH variable doesn't seem to contain /opt/jdk-17 but even after running source /etc/profile.d/jdk.sh and /opt/jdk-17 present, java --version is still using Java 16.


Solution

  • Based on the various comments ...

    1. You have Java 17 installed ... manually ... in /opt/jdk17
    2. You have JAVA_HOME pointing on the base of the Java 17 installation. I assume that there is (for example) a /opt/jdk17/bin/java executable.
    3. The Java 17 installation directory is NOT on the command search path (PATH).
    4. The search path is finding /usr/bin/java ... which (in your case) says Java 16 when you run java -version.
    5. Your system has the "/etc/alternatives" system installed, but sudo update-alternatives --config java says that only Java 16 is available.

    So ...

    The "alternatives" mechanism creates and maintains symlinks to various switchable commands. If you run ls -l /usr/bin/java for example, I expect that you will see that it is a symlink. When you run update-alternatives, it will attempt to update the symlinks. But it can only do this for commands and command versions that it knows about.

    Right now ... update-alternatives does not know about Java 17. It doesn't know it is installed, or where it is installed.

    If you had installed Java 17 from the package manager, then the config files that tell update-alternatives about Java 17 would have been added too.

    Solutions, ordered from "best" (1) to "worst".

    1. Remove your manual install of Java 17 and install it from the package manager. You might need to find / add an "experimental" Debian package repo to do this. (I get the impression that the official Debian repo managers tend to be rather slow in picking up new stuff.)

    2. Carefully read the documentation in man 1 update-alternatives and update-alternatives --help and then use the --install and --slave commands to tell it about Java 17.

    3. Find the Java symlinks and manually replace them with symlinks to the Java 17 versions of the executables. (Be careful ...)

    4. Add /opt/jdk17/bin to the start of your PATH. (Be careful ...)

    5. Just use the full pathnames; e.g. /opt/jdk17/bin/java rather than java.


    I also came across this:

    • Java 17 on the Raspberry Pi which includes (among other things) example commands for adding Java 17 to the alternatives system. It also mentions using sdkman.

    • How to Install Java 17 (JDK 17) on Debian 11. There is a comment that says:

      "Awesome! Thanks. This Debian package works on Raspberry Pi's Raspian 64-bit Bullseye as of posting. Only method that works without manually downloading packages and attempting to install. :)".

      But I see that you have a 32-bit Raspberry Pi ...