- 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
- How to change the default Java version on Ubuntu
- You might also like.
- How to set default Java version?
- 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
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.
How to change the default Java version on Ubuntu
If you are a Java developer, it is normal to have multiple Java versions installed on your machine to support different build environments. When a Java program is compiled, the build environment sets the oldest JRE version the program can support. Now, if you run this program on a Linux machine where an unsupported Java version is installed, you will encounter an exception.
For example, if your program is compiled on Java 11, it can’t be run on a machine where Java 8 is installed. But the good thing is you can install multiple Java versions on your machine and quickly change the default JRE version.
In this tutorial, I’ll explain how to change the default Java version on a Linux machine. First of all, run the following command to check the current Java version:
$ java -version openjdk version "1.8.0_191" OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.10.1-b12) OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
As you can see above, the default Java version is currently set to OpenJDK JRE 1.8. Now, let’s run the following command to see all available Java versions:
$ sudo update-alternatives --config java
Running the above command displays a list of installed Java JDKs and JREs allowing you to select the one as you want to set as default.
There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode * 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Press enter> to keep the current choice[*], or type selection number:
When prompted, select the Java version you would like to use. If the list does not include your desired Java version, you can always install it.
Now you can verify the default Java version as fellows:
$ java -version openjdk version "11.0.2" 2019-01-15 OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.10.3) OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.10.3, mixed mode, sharing)
That’s it. The default Java version is changed to OpenJDK 11.
If you frequently switch between different Java versions, it is a good idea to write a short script to automate the process. Here is the script I used for switching to OpenJDK 8 on my machine. java8.sh
sudo update-java-alternatives -s java-1.8.0-openjdk-amd64 export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64/ export PATH=$PATH:$JAVA_HOME
Similarly, you can create scripts for other Java versions installed on your machine. The next step is to add these scripts as aliases to .bashrc file.
... # Java Alias alias java8='source /opt/java/switch/java8.sh' alias java11='source /opt/java/switch/java11.sh'
Read Next: How to install Java on Ubuntu 18.04 ✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.
You might also like.
How to set default Java version?
I followed all the instructions stated at this question, but am encountering some problems with the last part of it. I actually have version 6.22 of java and would like to update to version 6.30. So after moving the extracted directory java-6-oracle into /usr/lib/jvm I do not know what to do, since the script that is pointed out in the answer above updates from java 5 to java 6. For sake of clearness here is output if I do an ls in dir /usr/lib/jvm :
$ ls -l /usr/lib/jvm total 8 lrwxrwxrwx 1 root root 14 2011-07-12 15:18 default-java -> java-6-openjdk lrwxrwxrwx 1 root root 14 2011-07-12 12:19 java-1.6.0-openjdk -> java-6-openjdk drwxr-xr-x 10 root root 4096 2012-04-12 12:06 java-6.31-oracle drwxr-xr-x 7 root root 4096 2012-02-24 14:43 java-6-openjdk
What should I do now? ADDED PART Under the suggestion of @fossfreedom I ran the script anyway and actually it updated the java version. In fact if I run command java -version output will be the following:
$ java -version java version "1.6.0_31" Java(TM) SE Runtime Environment (build 1.6.0_31-b04) Java HotSpot(TM) Server VM (build 20.6-b01, mixed mode)
There is still a problem, if Irun javac -version it gives me the old version installed:
$ javac -version javac 1.6.0_22
Java Version 1.6.0_22 from Sun Microsystems Inc.
What’s going wrong? It seems that Java Runtime Environment has updated, but Java Compiler and Java plugin for browser have not. How can I update them? OTHER ADDED PART sudo update-alternatives —config java will return following output
$ 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-6.31-oracle/bin/java 1062 auto mode 1 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 manual mode 2 /usr/lib/jvm/java-6.31-oracle/bin/java 1062 manual mode Press enter to keep the current choice[*], or type selection number:
these makes sense with the fact that JRE is correctly updated to version 6.31, issues are on Java Compiler and Java browser plugin. Any ideas?
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.