- Switch between multiple java versions
- 6 Answers 6
- Configuring Java
- Setting the JAVA_HOME Environment Variable
- Change default Java version in Ubuntu 20.04 LTS
- Install different versions of OpenJDK Development Kit and Java Runtime Environment
- Change default Java version in Ubuntu 20.04 LTS
- Switching between Java Versions on Ubuntu linux
- Ubuntu 20.04 / Debian — switch default java version
- Practical example
- Alternative titles
Switch between multiple java versions
While installing Android Studio on Ubuntu 14.04 I get the message that my Java version ( javac 1.7.0_79 ) is causing problems. I found a solution of how to install a newer Oracle version of Java:
sudo apt-add-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java8-installer
However I’m afraid that this might overwrite my existing open-jdk version of Java. Since I don’t know which of my programs depend on Java, I fear that this could crash these other programs. Is there a way to make sure apt-get doesn’t overwrite my previous Java? I would basically like to have installed both and be able to switch between them manually, depending on what version I need.
6 Answers 6
Apt-get won’t overwrite the existing java versions.
To switch between installed java versions, use the update-java-alternatives command.
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 ).
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 , . ).
tq this solve my problem too. @Nenotlep what I did is edit the environment variable manually either in .bashrc or /etc/environment
@Nenotlep in order to change JAVA_HOME you have to run the following command: source /etc/environment
@janb I honestly don’t understand why this would have any effect unless you manually specified JAVA_HOME in /etc/environment . I just switched back and forth between java 8 and 11, and there is still no JAVA_HOME in /etc/environment . In fact, it is set nowhere in my environment. I therefor rejected the suggested edit. If there are circumstances in which the suggested command is needed, please suggest a new edit and explain when and why it would be necessary.
My $JAVA_HOME is unaffected by any of the proposed commands, too. My ~/.bashrc has a line export JAVA_HOME=$(readlink -f /usr/bin/javac | sed «s:bin/javac::») though and that does the trick for me: source ~/.bashrc . If your ~/.bashrc doesn’t have that line, run directly export JAVA_HOME=$(readlink -f /usr/bin/javac | sed «s:bin/javac::») .
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 to keep. [*], or type selection number:
export JAVA_HOME="$(jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));')"
to set $JAVA_HOME from current active version
As noted by @danzel at the accepted answer, this affects only the java executable, not the other parts of the configuration. Use update-java-alternatives if available.
Configuring Java
You can configure which version is the default for use in the command line by using update-alternatives , which manages which symbolic links are used for different commands.
sudo update-alternatives --config java
The output will look something like the following.
There are 5 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 auto mode 1 /usr/lib/jvm/java-6-oracle/jre/bin/java 1 manual mode 2 /usr/lib/jvm/java-7-oracle/jre/bin/java 2 manual mode 3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode 4 /usr/lib/jvm/java-8-oracle/jre/bin/java 3 manual mode 5 /usr/lib/jvm/java-9-oracle/bin/java 4 manual mode Press to keep the current choice[*], or type selection number:
You can now choose the number to use as a default. This can also be done for other Java commands, such as the compiler ( javac ), the documentation generator ( javadoc ), the JAR signing tool ( jarsigner ), and more. You can use the following command, filling in the command you want to customize.
sudo update-alternatives --config command
Setting the JAVA_HOME Environment Variable
Many programs, such as Java servers, use the JAVA_HOME environment variable to determine the Java installation location.
Copy the path from your preferred installation and then open /etc/environment using Sublime Text or your favourite text editor.
At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path.
JAVA_HOME="/usr/lib/jvm/java-8-oracle"
Save and exit the file, and reload it: source /etc/environment .
You can now test whether the environment variable has been set by executing the following command: echo $JAVA_HOME . This will return the path you just set.
Change default Java version in Ubuntu 20.04 LTS
We consider this article to be an extension to the previous article – Install OpenJDK JRE / JDK in Ubuntu 20.04 LTS . As you would notice, we have already installed version 11 of OpenJDK JRE / JDK in Ubuntu 20.04 LTS release. Now, consider a scenario wherein we have also installed version 8 of OpenJDK JRE. In that case, the default version would still be the one which we have installed earlier i.e. version 11.0.7. So, in this article we would discuss how to change the default Java version in Ubuntu 20.04 LTS.
Just for clarity, we would first install OpenJDK Development Kit v11.0.7 and OpenJDK Java Runtime Environment v1.0.8_252. Thereafter, we would issue the specific command to change the default version of Java.
Note: Following operations would require you to have superuser privileges. In case you don’t have one, then contact your System Administrator for assistance.
Install different versions of OpenJDK Development Kit and Java Runtime Environment
Since the packages are already available in standard Ubuntu repository. Therefore, first update the Ubuntu repository to make the latest version of the package available. Hence, issue the following in terminal –
Now, to install OpenJDK Development Kit v11.0.7 –
sudo apt install openjdk-11-jdk
Next, to install OpenJDK Java Runtime Environment v1.8.0_252 –
sudo apt install openjdk-8-jre
Change default Java version in Ubuntu 20.04 LTS
First, verify for the Java version installed –
It would return the output –
openjdk version "11.0.7" 2020-04-14 OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1) OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)
Although we already installed version 1.8.0_252, But, when we pass the java -version command then it doesn’t mention the said version. Because, as of now v11.0.7 is the default version. To change that, we need to issue command –
sudo update-alternatives --config java
It would show us all the Java versions installed. Enter the selection number for OpenJDK JRE v1.8.0_252. Again –
This time it would return with –
openjdk version "1.8.0_252" OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1ubuntu1-b09) OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
In conclusion, we have discussed how to change the default version of Java in Ubuntu 20.04 LTS release.
Switching between Java Versions on Ubuntu linux
If you’re using Ubuntu Linux on your daily basis work, you’ve probably Java installed on your machine. Personally I prefer using Wepupd8 PPA to manage JAVA installation, it makes my life a lot more easier especially for updates. The Wepupd8 team didn’t add any binary for Oracle JAVA installation and they made a script to download the Oracle JAVA from Oracle website and install it straight away. So whenever Oracle will release the update, I can simple upgrade via package manager.
Working with multiple Java versions in your machine is a normal thing, especially if you’re a Java developer, and because I’m a (very) lazy person, I’m always looking for a quicker/easier way to make the switch.
Today, I’ll share with you my tip on this subject. First, let’s run the following command:
$ sudo update-alternatives —config java
Running this command shows a list of installed Java JDKs and JREs allowing one to be selected as the default that is used when java needs to be executed.
There are 5 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 auto mode 1 /usr/lib/jvm/java-6-oracle/jre/bin/java 1 manual mode 2 /usr/lib/jvm/java-7-oracle/jre/bin/java 2 manual mode 3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode 4 /usr/lib/jvm/java-8-oracle/jre/bin/java 3 manual mode 5 /usr/lib/jvm/java-9-oracle/bin/java 4 manual mode Press to keep the current choice[*], or type selection number:
But I’m using it just to get the Installation path of each Java version.
Then, for each version I created a script that contain the following lines (in the example below, I’m showing the one for java 8):
sudo update-java-alternatives -s java-8-oracle export JAVA_HOME=/usr/lib/jvm/java-8-oracle/ export PATH=$PATH:$JAVA_HOME
Note that I’m using u8.sh to make he switch to Java 8, and u9.sh for Java 9 and so on.
The final step is to add an alias in ~/.bashrc file to source our script as follow:
. # Alias alias u7='source /home/aboullaite/Utils/Java/u7.sh' alias u8='source /home/aboullaite/Utils/Java/u8.sh' alias u9='source /home/aboullaite/Utils/Java/u9.sh'
and That’s all. Now for switching between Java versions, I only run u8, u7 or u9 😉
Ubuntu 20.04 / Debian — switch default java version
jonrsharpe
In this short article, we would like to show how to switch the default Java version into a different one under Ubuntu 20.04 / Debian linux.
Quick solution (run the following command to open configuration):
sudo update-alternatives --config java
Note: command will list and ask us to select an active Java version.
Practical example
The example shows switching from Java 14 to Java 8.
$ java -version openjdk version "14.0.2" 2020-07-14 OpenJDK Runtime Environment (build 14.0.2+12-Ubuntu-120.04) OpenJDK 64-Bit Server VM (build 14.0.2+12-Ubuntu-120.04, mixed mode, sharing) john@john-pc:~$ sudo update-alternatives --config java There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-14-openjdk-amd64/bin/java 1411 auto mode * 1 /usr/lib/jvm/java-14-openjdk-amd64/bin/java 1411 manual mode 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Press to keep the current choice[*], or type selection number: 2 update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode john@john-pc:~$ java -version openjdk version "1.8.0_275" OpenJDK Runtime Environment (build 1.8.0_275-8u275-b01-0ubuntu1~20.04-b01) OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode) john@john-pc:~$