What should I set JAVA_HOME to using multiple jdks?

I want to switch between jdks below on Linux (using update-java-alternatives):

java-1.11.0-openjdk-amd64      1111       /usr/lib/jvm/java-1.11.0-openjdk-amd64
java-16-oracle                 1091       /usr/lib/jvm/java-16-oracle
java-1.8.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.8.0-openjdk-amd64

What should I set my JAVA_HOME to? Is it possible for update-java-alternatives to set JAVA_HOME?


I would suggest to use something like https://sdkman.io/ – it allows you to download and easy switch between various versions of JDK.

Example of the command to switch between the envs and the result:

➜ echo $JAVA_HOME

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

➜ sdk use java 8.0.242.hs-adpt

Using java version 8.0.242.hs-adpt in this shell.

➜ java -version               
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.242-b08, mixed mode)

➜ echo $JAVA_HOME