Set Java version with update-alternatives

Java runtime version can be switched without sudo as follows.

  1. (one-time) Setup your Bash shell to use ~/.local/bin instead of system-wide /usr

    mkdir ~/.local/bin
       

Edit ~/.bashrc, adding the line:

   export PATH="$HOME/.local/bin:$PATH"
   
  1. (one-time setup) enable switching Java version, for example

    update-alternatives --install $HOME/.local/bin/java java /usr/lib/jvm/java-11-openjdk-amd64/bin/java*
    20
    update-alternatives --install $HOME/.local/bin/java java /usr/lib/jvm/java-10-openjdk-amd64/bin/java*
    10
       
  2. At any time, switch Java version:

    update-alternatives --config java