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.
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:
Set java version as default (needs root permissions):
...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
which lists all installed versions with current active one marked and provides dialog to switch:
Use
to set
$JAVA_HOME
from current active versionConfiguring 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.The output will look something like the following.
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.Setting the
JAVA_HOME
Environment VariableMany 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.
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.Based on the answer from @muet, I found this to work seamlessly:
Add this to
~/.bashrc
:Add to aliases:
Then you can switch within the same shell using only:
useJava7
oruseJava8
jrunscript
may not be available in future releases, so safe choice is usingexport 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 usingupdate-alternative
command.Consider also using the GUI tool
galternatives
available through the ubuntu package manager.