139

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 ?

uraimo
  • 19,081
  • 8
  • 48
  • 55
Ashish Agarwal
  • 14,555
  • 31
  • 86
  • 125

9 Answers9

175

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.

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
srkiNZ84
  • 3,558
  • 5
  • 30
  • 37
  • 4
    Or /usr/lib/jvm/java-1.6.0-openjdk-amd64 if you're using OpenJDK instead of Oracle's (Sun) JDK. – pm_labs Nov 23 '12 at 09:07
  • 7
    You can use export JAVA_HOME=/usr/lib/jvm/default-java/ – noomz May 30 '13 at 04:25
  • 1
    @noomz How do you get that if default-java doesn't exist? – AHungerArtist Oct 09 '13 at 16:49
  • @AHungerArtist You get that if you install openjdk or other officially supported Ubuntu packages. Unfortunately, it's not supported for the Oracle JDK using the webupd8/java ppa. In fact, it's entirely unclear to me, what the `oracle-java7-set-default` package does. –  Mar 23 '14 at 19:12
  • Where in the file do you put that? – Czipperz Jan 10 '15 at 23:26
  • if you want it to work with Ubuntu's `update-alternatives` in most cases then I think the best (=least bad) option is to put unset JAVA_HOME to ~/.bashrc. The reason is that most programs try to figure out the correct JAVA_HOME on their own if it is not set and one of the first things they try is to look where the java executable points to. For the rest, you can set JAVA_HOME manually. Apparently Java is not important enough for Ubuntu, to support this out of the box: https://bugs.launchpad.net/ubuntu/+source/java-common/+bug/45348 – Jakub Kotowski Feb 19 '15 at 12:55
167

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
Flow
  • 23,572
  • 15
  • 99
  • 156
metdos
  • 13,411
  • 17
  • 77
  • 120
  • 27
    +1 This is the recommended way https://help.ubuntu.com/community/EnvironmentVariables – daniel kullmann Apr 04 '13 at 08:36
  • @metdos the above answer is not globally or at system level ? – Android Killer May 12 '13 at 12:43
  • 1
    @AndroidKiller did you check the link daniel kullmann pointed out? – metdos May 12 '13 at 17:30
  • @metdos Works nice, but referencing variables ain't working for me...`$JAVA_HOME` doesn't resolve when used like this `PATH=$JAVA_HOME/bin`, tried restarting, no effect. Do I have to `export` or `set` `JAVA_HOME` either in this file or my profile? – raffian May 29 '13 at 22:18
  • @Raffian I'm not sure about the different corner cases, this way it worked perfectly for me, for more information I would recommend you to read https://help.ubuntu.com/community/EnvironmentVariables – metdos May 30 '13 at 05:42
  • @raffian Why do you want to do this? Usually you define the java command using update-alternatives, which already links to the default Linux execution directory – keiki Aug 08 '13 at 12:21
63

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.

max
  • 2,627
  • 1
  • 24
  • 44
  • 1
    yes this solution is way cleaner than the accepted one. – simone cittadini Sep 29 '15 at 07:37
  • @JayminGajjar comments that express gratitude are not necessary. The mechanism of supporting good questions/answers in this website is to upvote them. If you do not posses enough reputation to do that but have enough to comment, please try to include some useful info in it like on what system and what OS did you test and find this solution working etc. – max Oct 16 '16 at 06:56
  • @Barracuda i already up-voted, You are correct about info, It worked for me on Ubuntu 16.10 64 Bit Server OS :) – Jaymin Gajjar Oct 19 '16 at 16:05
21

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

Johan
  • 74,508
  • 24
  • 191
  • 319
Jits
  • 9,647
  • 1
  • 34
  • 27
12

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

Roman Rhrn Nesterov
  • 3,538
  • 1
  • 28
  • 16
11

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::")
user987339
  • 10,519
  • 8
  • 40
  • 45
8

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.

Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871
6

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.

naki
  • 932
  • 6
  • 11
WillC
  • 957
  • 16
  • 19
  • Another approach: `java -XshowSettings 2>&1 | grep -e 'java.home' | awk '{print "JAVA_HOME="$3}' | sed "s/\/jre//g" >> /etc/environment` – Xdg May 31 '17 at 16:59
0

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.