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, ...).
Using multiple versions of Java in Ubuntu - Stack Overflow
linux - How can I have multiple versions of the Java JDK installed on my machine at the same time? - Stack Overflow
How can I install multiple java versions?
What is the point of all these new Java versions when Java 8 is all that seems to be supported by most apps?
Videos
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, ...).
Use
sudo update-alternatives --config java
which lists all installed versions with current active one marked and provides dialog to switch:
There are 3 choices for the alternative java (providing /usr/bin/java).
Selection Path...
------------------------------------------------------------
0 /usr/lib/jvm/java-9-oracle/bin/java...
* 1 /usr/lib/jvm/java-7-oracle/jre/bin/java...
2 /usr/lib/jvm/java-8-oracle/jre/bin/java...
3 /usr/lib/jvm/java-9-oracle/bin/java...
Press <enter> to keep...[*], or type selection number:
Use
export JAVA_HOME="$(jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));')"
to set $JAVA_HOME from current active version
type sudo update-alternatives --config java
The select the version you want.
And I think this question should be moved to askubuntu.
When you run java with no path, then your shell looks in your $PATH environment variable to find java.
If you want a specific java, you need to either change your $PATH, or run it with a path, like:
/usr/local/bin/java
or if you really want to cd there first, you could run
./java
from /usr/local/bin
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
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
Hi! I want to play minecraft but older versions of the game need old versions of java, how can I install one? Specifically java 8, I don't need it to be on PATH, only to be installed somewhere,