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.

7 Answers 7


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, ...).

  • 1
    For me this caused java -version to be java8, but JAVA_HOME was still set to 7 :( Commented Jul 14, 2016 at 11:42
  • 4
    @Nenotlep in order to change JAVA_HOME you have to run the following command: source /etc/environment
    – janb
    Commented Nov 16, 2018 at 10:23
  • 1
    @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.
    – danzel
    Commented Jun 10, 2020 at 14:24
  • 1
    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::").
    – Giszmo
    Commented Jun 18, 2020 at 14:27
  • 1
    set also accepts the name (full path not needed), so the following will also work: sudo update-java-alternatives --set java-1.17.0-openjdk-amd64
    – mrts
    Commented May 7 at 13:54


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: 


export JAVA_HOME="$(jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));')"

to set $JAVA_HOME from current active version

  • 2
    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. Commented Jan 10, 2019 at 20:59
  • 4
    Using this command, you have to update each of java, javac & jrunscript separately.
    – Stewart
    Commented Mar 16, 2020 at 10:19

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 <enter> 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.

sudo subl /etc/environment

At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path.


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.

  • This doesn't change the version of the java compiler javac Commented Oct 26, 2023 at 22:17

Based on the answer from @muet, I found this to work seamlessly:

Add this to ~/.bashrc:

export JAVA_HOME="$(jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));')"

Add to aliases:

alias useJava8='yes | sudo apt-get install oracle-java8-set-default && source ~/.bashrc'
alias useJava7='yes | sudo apt-get install oracle-java7-set-default && source ~/.bashrc'

Then you can switch within the same shell using only: useJava7or useJava8

  • to make aliases permanent one can put them in ~/.bashrc , more info here askubuntu.com/questions/17536/… Commented Feb 5, 2018 at 14:01
  • E: Unable to locate package oracle-java7-set-default
    – Prakash P
    Commented Mar 27, 2018 at 12:19
  • 1
    The unequivocally easiest way to install, use, and switch between different java versions is using SDKMAN! See here: sdkman.io/usage.html Commented Mar 28, 2018 at 12:51
  • recently come across another command to set the JAVA_HOME, yet to try on .bashrc: export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
    – ThiamTeck
    Commented Dec 8, 2018 at 2:53
  • oracle-java package does not exist. Use openjdk-8-jdk docs.datastax.com/en/jdk-install/doc/jdk-install/…
    – GilbertS
    Commented Nov 10, 2020 at 15:28

Consider also using the GUI tool galternatives available through the ubuntu package manager.


jrunscriptmay not be available in future releases, so safe choice is using export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java)))) as suggested by @ThiamTeck. Also .bashrc needs to be executed every time you change your java version using update-alternative command.


I have this in my .zshrc:

### Java
set_java_home() {
    export JAVA_HOME="$1"
    # Remove any existing java paths
    PATH=$(echo "$PATH" | sed -E -e 's|:/home/linuxbrew/.linuxbrew/opt/openjdk[^:]*/bin||g' -e 's|/home/linuxbrew/.linuxbrew/opt/openjdk[^:]*/bin:||g' -e 's|:/usr/local/jdk[^:]*/bin||g' -e 's|/usr/local/jdk[^:]*/bin:||g')
    # Add the new java path
    export PATH="$JAVA_HOME/bin:$PATH"
# Aliases to switch Java versions
alias java8='set_java_home $(brew --prefix openjdk@8)'
alias java11='set_java_home $(brew --prefix openjdk@11)'
alias java17='set_java_home $(brew --prefix openjdk@17)'
alias java21='set_java_home $(brew --prefix openjdk@21)'
# Set default Java version

Note: I used brew to install all LTS versions (brew install openjdk@{version})

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .