-1

I am installing Oracle JDK 8 in the machine as a root user in Ubuntu, But already Open JDK is installed in the machine.

# Install Oracle JDK
add-apt-repository ppa:webupd8team/java
apt-get -y update
echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 select true" | sudo debconf-set-selections
apt-get install -y --force-yes oracle-java8-installer

# set JAVA_HOME
JAVA_HOME=/usr/lib/jvm/java-8-oracle
echo "export JAVA_HOME=${JAVA_HOME}" >> /etc/profile
echo "export PATH=$JAVA_HOME/bin:$PATH" >> /etc/profile
source /etc/profile

echo "Oracle JDK installation completed"
echo "echo JAVA_HOME: $JAVA_HOME"

I want to use Oracle JDK 8 for all Java process.

At the end of script, I am print JAVA_HOME. It is printed as expected like below

echo JAVA_HOME: /usr/lib/jvm/java-8-oracle

After installation is completed, I am logging into the machine as normal user. I see JAVA_HOME is set as openjdk as below:-

 echo $JAVA_HOME
/usr/lib/jvm/java-8-openjdk-amd64

Even I logged in as root user and I see the same as above.

How can I set Oracle JDK 8 as JAVA_HOME across all users?

I am not sure, Will Java applications running in that machines using Oracle JDK or OpenJDK?

Galet
  • 5,853
  • 21
  • 82
  • 148
  • part of your installation writes to /etc/profile this makes it default for all users. – Oleg Sep 26 '17 at 06:21
  • @Oleg Then why its showing OpenJdk JAVA_HOME when everytime logging into the machine as normal or root users? – Galet Sep 26 '17 at 06:24
  • Then you have JAVA_HOME in some other init file set to openjdk – Oleg Sep 26 '17 at 06:27
  • @Oleg Yes. I have JAVA_HOME in /etc/environment. I have appended Oracle java home path to /etc/environment. But still it doesn't reflect oracle java home when I login every time. – Galet Sep 26 '17 at 06:48
  • There are other init files, there is probably some canonical answer about init files on askubuntu if not you should ask this question there. – Oleg Sep 26 '17 at 06:51
  • Possible duplicate of [How to set JAVA\_HOME in Linux for all users](https://stackoverflow.com/q/24641536/608639) – jww Jun 08 '19 at 10:24

1 Answers1

0

For ubuntu you can set the environment variable in

/etc/environment

Also you can have a look at update-alternatives command

Saqib Javed
  • 177
  • 4
  • 17
  • Even If I set Oracle java home in /etc/environment. It works when I do source /etc/environment But when I do re-login in to the machine, java home defaults to open jdk. – Galet Sep 26 '17 at 06:40
  • update-java-alternatives -l java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64 java-8-oracle 1081 /usr/lib/jvm/java-8-oracle root@ubuntu:~# update-java-alternatives -s java-8-oracle root@ubuntu:~# echo $JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64. Still I see Open JDK . – Galet Sep 26 '17 at 06:46
  • try with `sudo update-alternatives --config java` and `sudo update-alternatives --config javac` – Saqib Javed Sep 26 '17 at 06:50
  • also check the output of the `update-java-alternatives --list` – Saqib Javed Sep 26 '17 at 06:53
  • sudo update-alternatives --config java There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 auto mode 1 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode * 2 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual mode – Galet Sep 26 '17 at 07:07
  • sudo update-alternatives --config javac There are 2 choices for the alternative javac (providing /usr/bin/javac). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-8-oracle/bin/javac 1081 auto mode 1 /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 1081 manual mode * 2 /usr/lib/jvm/java-8-oracle/bin/javac 1081 manual mode – Galet Sep 26 '17 at 07:07
  • update-java-alternatives --list java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64 java-8-oracle 1081 /usr/lib/jvm/java-8-oracle – Galet Sep 26 '17 at 07:07
  • strange alternatives are set properly try `reboot` – Saqib Javed Sep 26 '17 at 07:10