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.
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 😉
How to use different java version by alias?
I have some tools that can only be run by java 8. So i downloaded java 8, but now i have two versions of java installed at the same time (os : Ubuntu):
java-1.11.0-openjdk-amd64 1101 /usr/lib/jvm/java-1.11.0-openjdk-amd64 java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64
What does type -a java say? It’s possible that you already have java that is a symlink to the specific java compiler.
3 Answers 3
You could do it in a couple of ways. The easiest way would be to put the following 2 lines in your profile initialization file:
alias java='/usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java' alias java8='/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin/java'
Other ways are depending on whether you are the admin of the machine or not. You can create soft links like this: /usr/bin/java -> /usr/lib/jvm/java-1.11.0-openjdk-amd64 (Not recommended since certain tools in your system may depend on /usr/bin/java ).
Update: Try to use sdkman if you can. Makes the job of installing java versions and setting up JAVA_HOME env variable simple.
would it not be better to have java point to the «/usr/bin/java» ? It would avoid any confusion around system updates etc.
Sometimes you may face issues if you have other tools that you /usr/bin/java and expect it be certain version but you changed it to point to version 11 or something. Generally, it is not such a big problem but you may face some subtle issues.
Aliases aren’t honored by scripts — much better to use the Debian alternatives system to select the interpreter to use, since that’ll work not just for direct invocations at a shell but for invocations from another program (like a build script or an IDE!) as well.
You can do this as well for JAVA_HOME:
alias java8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`" alias java11="export JAVA_HOME=`/usr/libexec/java_home -v 11`"
Updated: To run java directly use:
$ alias java8="`/usr/libexec/java_home -v 1.8`/bin/java" $ alias java11="`/usr/libexec/java_home -v 11`/bin/java" $ java8 -version java version "1.8.0_281" Java(TM) SE Runtime Environment (build 1.8.0_281-b09) Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode) $ java11 -version openjdk version "11.0.2" 2019-01-15 OpenJDK Runtime Environment 18.9 (build 11.0.2+9) OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
You can hardcode your path as well.
I have some tools that can only be run by java 8. So I downloaded java 8, but now I have two versions of java installed at the same time (os : Ubuntu).
You could do this using aliases (see other answers) but this has the problem that only a shell will respect an alias . and then only if it has the alias defined or loaded by the shell:
- Aliases are not inherited via the environment.
- Whether a tool’s launch script will see the aliases will typically depend on whether you put the alias definitions into the correct shell init file.
- It is also possible that an application’s launch script will assume that java8 or java11 is a genuine (absolute or relative) pathname, and try to resolve it via the file system.
In short, there is a good chance that using an alias won’t work in your use-case.
So a better idea is to arrange that the appropriate Java is on the Command Search Path. You could do this a number of different ways:
- You could use the Ubuntu «alternatives» system to globally switch between Java 8 and Java 11.
- You could update the PATH variable so that (for example) the Java 11 executables come before the Java 8 executables. This could be done in shell init files (per user or globally), in the application launch scripts . or by hand.
- You could even replace «/usr/bin/java» or whatever with a direct symlink to the version you want to use. (This is crude, and I wouldn’t recommend it.) Note that the Java command entries in «/usr/bin» are probably already symlinks.)