Installing Multiple Java Versions On Linux Mint 20

Nuwan Chamara
2 min readJan 7, 2021
Photo by Michiel Leunens on Unsplash

This is how you should Install multiple Java Versions On Linux Mint 20 using alternatives. Alternatives is an application installed by default in Mint os.

  1. Install default java version

apt install default-jdk

2. Install java 8

apt install openjdk-8-jdk

3. View isntalled java versions

update-java-alternatives — list

My out put is two java versions as following
java-1.11.0-openjdk-amd64 1111 /usr/lib/jvm/java-1.11.0-openjdk-amd64
java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64

4. Check current version

java -version

mine is java 11

5. Change java version

update-alternatives — config java

and select the required version number

All set now can use multiple java versions and switch them easily.

Ref: https://techviewleo.com/install-java-openjdk-on-linux-mint/

How to set JAVA_HOME is bit tricky and there is no proper solution. So here is my workaround add the following to the bachrc

javapath=$( readlink --canonicalize /usr/bin/java)
removebin="/bin/java"
removejre="/jre"
javapath2=${javapath/$removebin/}
export JAVA_HOME=${javapath2/$removejre/}

Then do source to reload the JAVA_HOME whenever you change the java version using alternatives

source ~/.bashrc

Explanation: What I have done is get the java classpath from the variable set by update-alternatives app and then remove the bin/java part from it and then assign it to the JAVA_HOME. This process happens at login to the system. if you alter the java version in the middle of a session you will have to reload the profile.

--

--