Java Install and switch when multiple versions exist
I’ve got multiple versions of Java installed and want/need to use different versions for different applications I’m supporting.
Example Problem
I have multiple versions of Java installed and need to use different versions depending on the source code I’m editing.
- App1J8 - Uses Java 8
- App2J11 - Uses Java 11
- App3J17 - Uses Java 17
Solution
There are different ways people have done this. One solution uses Linux
Manually
I often just manually install the Java JDK and manually switch to the version I want
to use. I define a function in my .bashrc
file and then call it when starting bash.
function setJAVA_HOME(){
:
# Run "update-alternatives --list java" to see what's installed
# and "sudo update-alternatives --config java" to update
#
# Use whatever version of Java my system is pointing to
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::" )
# Force java8 to be used
# export JAVA_HOME=/etc/alternatives/java_sdk_1.8.0_openjdk
# export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/
# export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
# export JAVA_HOME=/usr/lib/jvm/java-14-openjdk-amd64
export JAVA_HOME=$HOME/.jdks/jdk-17.0.13
# export JAVA_HOME=$HOME/.jdks/jdk-23.0.1
pjs_addPath $JAVA_HOME/bin
}
Using UNIX Alternatives
See https://www.redhat.com/en/blog/alternatives-command for a intro to this command.
Debian update-alternatives
Debian unix uses update-alternatives which is like the original
To switch between different versions that have been installed use
# List the choices of Java Compiler installed and registered with alternatives
update-alternatives --list javac
# Pick a different version
update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/javac 1111 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/javac 1111 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 1081 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Installing JDKs
From Apt
On Ubuntu 20 and 22, you can install Java using this command. The problem is you don’t know what version will be installed.
sudo apt install default-jdk
Manually Downloading
Depending on which version of Oracle Java you install, you might need to sign away your life (jk, but you can’t just download it).
I’ve downloaded
- JDK8 - https://www.oracle.com/java/technologies/downloads/#java8?er=221886
- JDK11 - https://www.oracle.com/java/technologies/javase/jdk11-archive-downloads.html
- JDK17 - https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html
- JDK21 - https://www.oracle.com/java/technologies/javase/jdk21-archive-downloads.html
- JDK23 - https://www.oracle.com/java/technologies/javase/jdk23-archive-downloads.html
I download the TAR.GZ formatted file and then untar to $HOME/.jdks or $HOME/.apps.
Then make symlinks (if necessary) to simpler names.
cd $HOME/.jdks
tar tzf $HOME/Downloads/jdk-21_linux-x64_bin.tar.gz | head
tar xzf $HOME/Downloads/jdk-21_linux-x64_bin.tar.gz
ln -s jdk-21.0.5 jdk-21
Ubuntu 22 Linux using apt
Also see https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-on-ubuntu-22-04
sudo apt install -y openjdk-8-jdk
sudo apt install openjdk-11-jre-headless
sudo apt install openjdk-17-jre-headless
Following instructions from Google’ing
I found this when seraching
- https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-on-ubuntu-22-04
-
https://askubuntu.com/questions/48468/how-do-i-install-java