How to set Oracle's Java as the default Java in Ubuntu?

JavaUbuntuJava Home

Java Problem Overview


How do I change the value of JAVA_HOME in Ubuntu to point to Oracle's Java?

Should it point to java-6-sun or java-6-sun-1.6.0.24 ?

Java Solutions


Solution 1 - Java

I put the line:

export JAVA_HOME=/usr/lib/jvm/java-7-oracle

in my ~/.bashrc file.

/usr/lib/jvm/java7-oracle should be a symbolic link pointing to /usr/lib/jvm/java-7-oracle-[version number here].

The reason it's a symbolic link is that in case there's a new version of the JVM, you don't need to update your .bashrc file, it should automatically point to the new version.

If you want to set JAVA_HOME environment variables globally and at system level means use should set in /etc/environment file.

Solution 2 - Java

If you want to change it globally and at system level;

In

/etc/environment

add this line:

JAVA_HOME=/usr/lib/jvm/java-7-oracle

Solution 3 - Java

to set Oracle's Java SE Development Kit as the system default Java just download the latest Java SE Development Kit from here then create a directory somewhere you like in your file system for example /usr/java now extract the files you just downloaded in that directory:

$ sudo tar xvzf jdk-8u5-linux-i586.tar.gz -C /usr/java

now to set your JAVA_HOME environment variable:

$ JAVA_HOME=/usr/java/jdk1.8.0_05/
$ sudo update-alternatives --install /usr/bin/java java ${JAVA_HOME%*/}/bin/java 20000
$ sudo update-alternatives --install /usr/bin/javac javac ${JAVA_HOME%*/}/bin/javac 20000

make sure the Oracle's java is set as default java by:

$ update-alternatives --config java

you get something like this:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                           Priority   Status
------------------------------------------------------------
* 0            /opt/java/jdk1.8.0_05/bin/java                  20000     auto mode
  1            /opt/java/jdk1.8.0_05/bin/java                  20000     manual mode
  2            /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java   1061      manual mode

Press enter to keep the current choice[*], or type selection number:

pay attention to the asterisk before the numbers on the left and if the correct one is not set choose the correct one by typing the number of it and pressing enter. now test your java:

$ java -version

if you get something like the following, you are good to go:

java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) Server VM (build 25.5-b02, mixed mode)

also note that you might need root permission or be in sudoers group to be able to do this. I've tested this solution on both ubuntu 12.04 and Debian wheezy and it works in both of them.

Solution 4 - Java

If you want this environment variable available to all users and on system start then you can add the following to /etc/profile.d/java.sh (create it if necessary):

export JDK_HOME=/usr/lib/jvm/java-7-oracle
export JAVA_HOME=/usr/lib/jvm/java-7-oracle

Then in a terminal run:

sudo chmod +x /etc/profile.d/java.sh
source /etc/profile.d/java.sh

> My second question is - should it point to java-6-sun or java-6-sun-1.6.0.24 ?

It should always point to java-7-oracle as that symlinks to the latest installed one (assuming you installed Java from the Ubuntu repositories and now from the download available at oracle.com).

Solution 5 - Java

java 6

export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-amd64

or java 7

export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64

Solution 6 - Java

If you're doing any sort of development you need to point to the JDK (Java Development Kit). Otherwise, you can point to the JRE (Java Runtime Environment).

The JDK contains everything the JRE has and more. If you're just executing Java programs, you can point to either the JRE or the JDK.

You should set JAVA_HOME based on current Java you are using. readlink will print value of a symbolic link for current Java and sed will adjust it to JRE directory:

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")

If you want to set up JAVA_HOME to JDK you should go up one folder more:

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:jre/bin/java::")

Solution 7 - Java

If you want to use specific version of Java when multiple JDKs are installed, just setting JAVA_HOME may not work.

You need to use sudo update-alternatives --config java to set default Java.

Refer to https://askubuntu.com/questions/121654/how-to-set-default-java-version.

Solution 8 - Java

See this; run

sudo  update-java-alternatives --list

to list off all the Java installations on a machine by name and directory, and then run

sudo  update-java-alternatives --set [JDK/JRE name e.g. java-8-oracle]

to choose which JRE/JDK to use.

If you want to use different JDKs/JREs for each Java task, you can run update-alternatives to configure one java executable at a time; you can run

sudo  update-alternatives --config java[Tab]

to see the Java commands that can be configured (java, javac, javah, javaws, etc). And then

sudo  update-alternatives --config [javac|java|javadoc|etc.]

will associate that Java task/command to a particular JDK/JRE.

You may also need to set JAVA_HOME for some applications: from this answer you can use

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")

for JREs, or

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:jre/bin/java::")

for JDKs.

Solution 9 - Java

Download the desired Java Version from Here

  1. wget -c --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" http://monalisa.cern.ch/MONALISA/download/java/jdk-8u172-linux-x64.tar.gz

  2. cd /opt/

  3. tar -xvzf jdk-8u172-linux-x64.tar.gz 4.JAVA_HOME=/opt/jdk1.8.0_172/

  4. update-alternatives --install /usr/bin/java java ${JAVA_HOME%*/}/bin/java 20000

  5. update-alternatives --install /usr/bin/javac javac ${JAVA_HOME%*/}/bin/javac 20000

  6. update-alternatives --config java

  7. Choose the version that you want to use. For example: Number 1 will indicate '/opt/jdk1.8.0_172/bin/java' iTerm

  8. Validate the java version: java -version

    Final step:

    Restart the service.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAshish AgarwalView Question on Stackoverflow
Solution 1 - JavasrkiNZ84View Answer on Stackoverflow
Solution 2 - JavametdosView Answer on Stackoverflow
Solution 3 - JavamokiView Answer on Stackoverflow
Solution 4 - JavaJitsView Answer on Stackoverflow
Solution 5 - JavaRoman Rhrn NesterovView Answer on Stackoverflow
Solution 6 - Javauser987339View Answer on Stackoverflow
Solution 7 - JavaprosseekView Answer on Stackoverflow
Solution 8 - JavaWillCView Answer on Stackoverflow
Solution 9 - JavaLidor EttingerView Answer on Stackoverflow