Apt-get won't overwrite the existing java versions.

To switch between installed java versions, use the update-java-alternatives command.

List all java versions:

update-java-alternatives --list

Set java version as default (needs root permissions):

sudo update-java-alternatives --set /path/to/java/version

...where /path/to/java/version is one of those listed by the previous command (e.g. /usr/lib/jvm/java-7-openjdk-amd64).


Additional information:

update-java-alternatives is a convenience tool that uses Debian's alternatives system (update-alternatives) to set a bunch of links to the specified java version (e.g. java, javac, ...).

Answer from danzel on askubuntu.com
🌐
GitHub
gist.github.com › ofhouse › b6cca8006329107fa0770a8dd87f45cf
Install and manage multiple Java SDK versions on Ubuntu · GitHub
Done openjdk-11-jre/focal-updates,focal-security 11.0.8+10-0ubuntu1~20.04 amd64 OpenJDK Java runtime, using Hotspot JIT openjdk-13-jre/focal-updates 13.0.3+3-1ubuntu2 amd64 OpenJDK Java runtime, using Hotspot JIT openjdk-14-jre/focal-updates 14.0.1+7-1ubuntu1 amd64 OpenJDK Java runtime, using Hotspot JIT openjdk-8-jre/focal-updates,focal-security,now 8u265-b01-0ubuntu2~20.04 amd64 [installed,automatic] OpenJDK Java runtime, using Hotspot JIT ... java -version # Output for OpenJDK 8 > openjdk version "1.8.0_265" OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0ubuntu2~20.04-b01) OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode) # Output for OpenJDK 11 > openjdk version "11.0.8" 2020-07-14 OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04) OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)
🌐
Batsov
batsov.com › articles › 2021 › 12 › 10 › working-with-multiple-versions-of-java-on-ubuntu
Working with Multiple Versions of Java on Ubuntu | (think)
December 10, 2021 - I realized I had forgotten (once again) how to switch between multiple Java versions on Ubuntu (Debian), so I’ve decided to write a short article that would help me remember this better.1 · You can easily install multiple versions of Java on Ubuntu via apt:
Discussions

Using multiple versions of Java in Ubuntu - Stack Overflow
I have a Ubuntu machine where I already have one JDK version which is installed. Next I have downloaded the second version into /usr/local. Now, I want to run a program against the second JDK which is in /usr/local, i.e., I will copy a sample .java program in /usr/local/bin and then execute it. More on stackoverflow.com
🌐 stackoverflow.com
linux - How can I have multiple versions of the Java JDK installed on my machine at the same time? - Stack Overflow
Background I work mostly in Java and Scala on my job, but a lot of our legacy code has not yet been updated to the most recent JDK. Thus, we have to have JDK versions 5, 6, 7, and 8 installed on our More on stackoverflow.com
🌐 stackoverflow.com
How can I install multiple java versions?
emerge dev-java/openjdk:8 emerge dev-java/openjdk:11 emerge dev-java/openjdk:17 emerge dev-java/openjdk:21 Should be somewhere below /opt then. More on reddit.com
🌐 r/Gentoo
17
5
December 16, 2023
What is the point of all these new Java versions when Java 8 is all that seems to be supported by most apps?
Because they switched to a new release model which allows for them to get new features out faster. Same thing happened when Firefox and chrome switched to a timed release model. People bitched about version numbers but slowly got use to the fact that "I don't really care that this is firefox 82". From an organization standpoint, this has been great for the Java getting features out. It used to be that they'd say "Ok, we are going to target key feature X for java Y". The problem with that is nearly all project development got devoted to that feature and ultimately "when" java would go out was completely unknown. Further, releases where crazy times for JDK developers. With the new model, they've been able to work on a lot more features in parallel. A ton of usability features made it in (records, pattern matching, string literals) which never would have made it in the old model. Further, it's allowed them to slowly walk in huge features (Valhalla, loom, etc) making pre-emptive changes needed to support massive changes in the future. Post Java 9, the upgrades have been a breeze. Honestly, I almost wish they'd move to an even faster release model to really force people to stop caring so much about these version numbers. Rust does a release every 6 weeks and I think that works great. Nobody cares that it's now Rust 1.50 or whatever. More on reddit.com
🌐 r/java
45
0
April 19, 2021
🌐
Medium
medium.com › javarevisited › install-multiple-versions-of-java-7-8-11-on-ubuntu-20-04-beginners-guide-1368a230eafd
How to install multiple versions of Java on Ubuntu 20.04 — Beginners Guide | by Hasith Magage | Javarevisited | Medium
August 4, 2022 - sudo update-alternatives — install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_80/bin/java 1065sudo update-alternatives — install /usr/bin/javac javac /usr/lib/jvm/jdk1.7.0_80/bin/javac 1065sudo update-alternatives — install /usr/bin/jar jar /usr/lib/jvm/jdk1.7.0_80/bin/jar 1065sudo update-alternatives — install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.7.0_80/bin/javaws 1065 · Switch the default version of java as you need.
🌐
FOSS Linux
fosslinux.com › home › linux distributions › debian & ubuntu › ubuntu › how to install & switch java versions on ubuntu (2026)
How to Install & Switch Java Versions on Ubuntu (2026)
June 1, 2026 - Need multiple Java versions on Ubuntu? I show you how to install OpenJDK 25, Java 21 LTS, and switch between them using update-alternatives and SDKMAN.
🌐
OneUptime
oneuptime.com › home › blog › how to install and switch java versions on ubuntu
How to Install and Switch Java Versions on Ubuntu
March 2, 2026 - # Download the Oracle JDK 21 Debian package for Linux x64 wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb # Install Oracle JDK 21 sudo dpkg -i jdk-21_linux-x64_bin.deb # If dpkg reports missing dependencies, fix them sudo apt -f install · update-alternatives manages multiple versions of the same command:
🌐
LinuxCloudVPS
linuxcloudvps.com › home › how to install and manage multiple java versions on linux
How to Install and Manage Multiple Java Versions on Linux | LinuxCloudVPS Blog
August 29, 2023 - Oracle JDK is the closed-source version of Java developed by Oracle. This tutorial will use the latest Ubuntu 22.04 OS, but you can choose any Linux distro. Java is compatible with all Linux distros. Installing multiple versions of Java and switching between them is a straightforward process.
Find elsewhere
🌐
TecAdmin
tecadmin.net › managing-multiple-java-versions-on-ubuntu
Managing Multiple Java Versions on Ubuntu – TecAdmin
April 26, 2025 - Oracle releases new versions regularly, and each version may have differences in performance, security, and functionality. Commonly used versions include Java 8 (LTS), Java 11 (LTS), and the latest release at the time of writing. Ubuntu’s package manager, APT, makes it easy to install multiple Java versions.
🌐
DEV Community
dev.to › thegroo › install-and-manage-multiple-java-versions-on-linux-using-alternatives-5e93
Install and manage multiple Java versions on Linux using alternatives - DEV Community
February 10, 2022 - Make sure to have wget installed. ... Extract it to /usr/lib/jvm/open-jdk-11 folder you have just created. tar -xzf ./openjdk-11+28_linux-x64_bin.tar.gz -C /usr/lib/jvm/open-jdk-11 --strip-components=1 · Update alternatives to add java, javac, jshell and jar ... Hit Ctrl + D to exit. If you have multiple versions of java installed using this same process above, you can just switch between them using alternatives,
🌐
Petewan
petewan.com › home
Install and manage multiple Java JDK and JRE versions on Ubuntu 20.04 – Pete Wan Blog
November 1, 2020 - When the java application does not use a graphical user interface the headless version can be used. ... Command 'javac' not found, but can be installed with: sudo apt install default-jdk # version 2:1.11-72, or sudo apt install openjdk-11-jdk-headless # version 11.0.8+10-0ubuntu1~20.04 sudo apt install openjdk-13-jdk-headless # version 13.0.4+8-1~20.04 sudo apt install openjdk-14-jdk-headless # version 14.0.2+12-1~20.04 sudo apt install openjdk-8-jdk-headless # version 8u265-b01-0ubuntu2~20.04 sudo apt install ecj # version 3.16.0-1
Top answer
1 of 3
8

Solution

Apparently, the RPM scripts that Oracle releases tend to break any previous JDK versions you have installed by way of removing key JAR files from those older installations, such as tools.jar and rt.jar. This renders the formerly functioning JDK useless for developers.

My first response was to rename the installation directory prior to running the second installer script. But doing that made RPM complain about already having a version of the JDK installed when running the second script. So the hack of the hack is to install one version, rename that directory, then "remove" that version with RPM (even though it will complain about not being able to find any of the respective files associated with it). Then you are free to install the second version, repeating the previous steps for each one until you get to the latest version you wish to install.

TL;DR

Here are the steps to get multiple working JDK versions on the same machine:

1. Install the first version of the JDK using the installer script:

:~# chmod a+x jdk-1_5_0_22-linux-amd64-rpm.bin
:~# ./jdk-1_5_0_22-linux-amd64-rpm.bin -x
:~# rpm -ivh --replacepkgs jdk-1_5_0_22-linux-amd64.rpm

2. Rename the install directory:

:~# mv /usr/java/jdk1.5.0_22 /usr/java/jdk1.5.0_22-bak

3. Use RPM to uninstall that version of the JDK:

:~# rpm -e jdk-2000:1.5.0_22-fcs.x86_64

4. Rinse and repeat until all versions are installed

5. Remove the -bak from the end of the directory names

:~# mv /usr/java/jdk1.5.0_22-bak /usr/java/jdk1.5.0_22

6. Change the /usr/java symlinks to the correct target:

:~# ls -la /usr/java
default -> java7
java5 -> jdk1.5.0_22
java6 -> jdk1.6.0_45
java7 -> jdk1.7.0_80
java8 -> jdk1.8.0_101
jdk1.5.0_22
jdk1.6.0_45
jdk1.7.0_80
jdk1.8.0_101
latest -> java8

7. Change the /usr/bin symlinks to the correct target:

:~# ls -la /usr/bin/ja*
jar -> /usr/java/default/bin/jar
java -> /usr/java/default/bin/java
javac -> /usr/java/default/bin/javac
javadoc -> /usr/java/default/bin/javadoc
javah -> /usr/java/default/bin/javah
javaws -> /usr/java/default/bin/javaws
2 of 3
3

On ubuntu/linux you can switch java version using

update-alternatives --config java

But before, you need install the version.

You can use this script (./install-java.sh) to install multiple JVMs

#!/bin/sh
(
lookforJdks=$PWD
echo "Directory: $lookforJdks"
jdks=`test -e ./javac || find $lookforJdks -type d -iname '*jdk1.*' 2> /dev/null`
#set -e
echo 'which jdk do you want to choose? looking for jdks. This might take a while'
echo "$jdks" | awk '{printf("%5d : %s\n", NR,$0)}'
read choose
test -e ./javac || cd `echo "$jdks" | tr '\n' ',' | cut -d',' -f $choose`/bin
for e in appletviewer extcheck idlj jar jarsigner java javac javadoc javah javap jconsole \
 jdb jhat jinfo jmap jps jrunscript jsadebugd jstack jstat jstatd native2ascii rmic \
 schemagen serialver wsgen wsimport xjc jvisualvm jmc; do sudo update-alternatives \
 --install /usr/bin/$e $e $(readlink -f ./$e) 100; done
)

echo "RUN update-alternatives --config java"

Put this script in folder where has unpacked the JVM(s), an run:

/opt/install-java.sh

Next use: update-alternatives --config java

🌐
Faun
faun.pub › how-to-install-and-manage-multiple-jdk-versions-on-ubuntu-22-04-3de225c7d8c8
How to Install and Manage Multiple JDK Versions on Ubuntu 22.04 | by Achini Udari Jayasena | FAUN.dev() 🐾
November 1, 2024 - You can install it by running: ... mixed mode, sharing) With multiple JDKs installed, you can use the `update-alternatives` tool to switch between them....
🌐
Dotlinux
dotlinux.net › blog › how-to-install-and-switch-java-versions-on-ubuntu-linux
How to Install and Switch Java Versions on Ubuntu Linux
... sudo add-apt-repository ppa:linuxuprising/java sudo apt update sudo apt install oracle-java17-installer · Accept the Oracle license when prompted. Once you have multiple Java versions installed, use update-alternatives to switch between them.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-install-java-with-apt-on-ubuntu-22-04
How to Install Java with Apt on Ubuntu (JRE & JDK) | DigitalOcean
February 2, 2026 - You can install a specific version of Java on your Ubuntu system by using the apt command. For example, to install Java 11, you can run the following command: ... If you have multiple versions of Java installed on your system, you can use the update-alternatives command to switch between them.
🌐
The Geek Diary
thegeekdiary.com › how-to-work-with-multiple-java-versions-under-linux
How to work with multiple java versions under Linux
GitLab CI/CD is a tool used for automating the process of testing, building, and deploying code. It helps developers integrate and deliver software quickly and reliably. Here’s a breakdown of the p · © 2025 Sandeep. Some rights reserved