Home > Software design >  What should I set JAVA_HOME to using multiple jdks?
What should I set JAVA_HOME to using multiple jdks?

Time:10-17

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?

CodePudding user response:

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
/home/pdys/.sdkman/candidates/java/current

~ 
➜ 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             
/home/pdys/.sdkman/candidates/java/8.0.242.hs-adpt

CodePudding user response:

What should I set my JAVA_HOME to?

Respectively:

   export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64

   export JAVA_HOME=/usr/lib/jvm/java-16-oracle

   export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64

Is it possible for update-java-alternatives to set JAVA_HOME?

No. It isn't.

The purpose of update-java-alternatives is to create / maintain the symlinks in /usr/bin etcetera which determine what the "global" settings are. The JAVA_HOME environment variable is for per-user / per-script (or per-session) configuration. They are orthogonal.

I guess you could write a shell function that sets JAVA_HOME to (say) the Java installation that corresponds to whatever /usr/bin/java currently resolves to.

  • Related