The Azul website is the place to be to download open source Java SDK's.
To fetch a suitable Java SDK for the Raspberry Pi, do the following:
Go to the Azul website
Select the button Download now (upper right corner)
Move down the new page until the section Download Azul Zulu Builds of OpenJDK
Select Arm 32-bit HF in the Architecture selection box. As of this writing, there will be 3 possible JDK's available:
Java 17 (MTS)
Java 11 (LTS) ==> Used by Pi4J 1.4
Java 8 (LTS) ==> Used by Pi4J 1.2
Let's take Java 8 as an example here, so download the corresponding .tar.gz file
Once downloaded, grab a tool (e.g. WinSCP on Windows) to copy the tar file to a location on the Raspberry Pi, e.g. ~/mystuff/azul
Once the tar file is on the Raspberry Pi, untar using tar xfvz <name_of_tar.gz_file>
Now do the same for Java 11.
You will have two .tar.gz files and two expanded directories on the Raspberry Pi like so:
pi@domohome:~/mystuff/azul $ lla
total 287576
4 drwxr-xr-x 4 pi pi 4096 Dec 27 17:50 .
4 drwxr-xr-x 8 pi pi 4096 Dec 6 17:40 ..
4 drwxr-xr-x 10 pi pi 4096 Jul 13 15:18 zulu11.50.19-ca-jdk11.0.12-linux_aarch32hf
4 drwxr-xr-x 9 pi pi 4096 Oct 12 03:53 zulu8.58.0.13-ca-jdk8.0.312-linux_aarch32hf
174112 -rw-r--r-- 1 pi pi 178288904 Sep 17 14:45 zulu11.50.19-ca-jdk11.0.12-linux_aarch32hf.tar.gz
113448 -rw-r--r-- 1 pi pi 116166108 Nov 14 10:33 zulu8.58.0.13-ca-jdk8.0.312-linux_aarch32hf.tar.gz
Rename the two directories to jdk11.0.12 and jdk8.0.312 respectively
Copy both directories as sudo to /usr/lib/jvm:
sudo cp -R jdk11.0.12 /usr/lib/jvm
and
sudo cp -R jdk8.0.312 /usr/lib/jvm
Then follow these guidelines on how to use the Linux update-alternatives to install and switch between the two JDKs on the Raspberry Pi.
Same mechanism is to be used to download the correct JDK for Windows. Just select the corresponding OS and choose your JDK.
How to cope with different Java versions on your Linux distro? Well, I found a nice explanation here, which is actually working too!
Situation:
I have downloaded 2 Linux distributions from the Azul website: jdk8.0.312 (a.k.a. JDK 1.8) and jdk11.0.12; see here for more info on how to fetch the respective JDK's from the Azul website.
I downloaded both .tar.gz files into ~/mystuff/azul; the following are the .tar.gz files:
zulu8.58.0.13-ca-jdk8.0.312-linux_aarch32hf.tar.gz
zulu11.50.19-ca-jdk11.0.12-linux_aarch32hf.tar.gz
I then untarred the files using the command tar xfvz <.tar.gz>; this resulted in two directories having the same name as the .tar.gz files.
I renamed both directories to jdk8.0.312 and jdk11.0.12 respectively.
As sudo, I then copied both directories to /usr/lib/jvm
I then ran the following two commands to "register" the two JDK versions (see StackOverflow):
For JDK 8.0.312:
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk8.0.312/bin/java" 1
(1 is the priority given: lower number is higher priority)
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk8.0.312/bin/javac" 1
For JDK 11.0.12:
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk11.0.12/bin/java" 4000
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk11.0.12/bin/javac" 4000
Now that the two JDK's are installed, we can switch between the two using the following command:
sudo update-alternatives --config java
This will show you an overview of the installed JDK's and will ask you to give the number of the JDK you want to activate from this moment onwards.
Below is an example of such overview:
pi@domohome:/usr/lib/jvm $ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/jdk11.0.12/bin/java 4000 auto mode
1 /usr/lib/jvm/jdk11.0.12/bin/java 4000 manual mode
2 /usr/lib/jvm/jdk8.0.312/bin/java 1 manual mode
Press <enter> to keep the current choice[*], or type selection number:
If you select, say, 2 and you then run the command java -version, you should see the following:
pi@domohome:/usr/lib/jvm $ java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (Zulu 8.58.0.13-CA-linux_aarch32hf) (build 1.8.0_312-b07)
OpenJDK Client VM (Zulu 8.58.0.13-CA-linux_aarch32hf) (build 25.312-b07, mixed mode)
Running sudo update-alternatives --config java again and selecting 0 will give you the following info when running java -version:
pi@domohome:/usr/lib/jvm $ java -version
openjdk version "11.0.12" 2021-07-20 LTS
OpenJDK Runtime Environment Zulu11.50+19-CA (build 11.0.12+7-LTS)
OpenJDK Client VM Zulu11.50+19-CA (build 11.0.12+7-LTS, mixed mode)
This is how you can install and switch between 2 or more JDK's.
Note: to make life easy, you can create an alias to switch JDK versions in the ~/.bash_aliases file like so:
alias switchjava='sudo update-alternatives --config java'
This way, you don't have to type the long command every time you want to switch JDK's..
Some more useful update-alternatives commands can be found here.
Fiddling with different parameters to switch between Java versions can be a pain in the but. An alternative to switch between different Java versions is SDKMAN. SDKMAN is a nice tool to switch quickly between different versions of Java on Linux.
See this web page for more info about SDKman