1

I recently tried installing the most recent version of Oracle JDK (1.8) but somehow I'm unable to overwrite the current version of java being used by the system. I set the JAVA_HOME variable to point to the new installation, added it to the path and exported the variables, as shown below:

Contents of /etc/profile:

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "$PS1" ]; then
  if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

# The default umask is now handled by pam_umask.
# See pam_umask(8) and /etc/login.defs.

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi
JAVA_HOME=/usr/local/java/jdk1.8.0_40
PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME
export PATH 
~        

Output of environment variables:

echo $JAVA_HOME
/usr/local/java/jdk1.8.0_40

echo $PATH 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/java/jdk1.8.0_40/bin

But here's the issue:

java -version
java version "1.7.0_72"
Java(TM) SE Runtime Environment (build 1.7.0_72-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.72-b04, mixed mode)

which java
/usr/bin/java

javac -version
javac 1.7.0_72

'java -version' and javac are still pointing to the original installation. I installed gradle (for which I made the upgrade in the first place), which points to the newer installation.

gradle --version

------------------------------------------------------------
Gradle 1.5
------------------------------------------------------------

Gradle build time: Sunday, June 15, 2014 3:27:36 PM UTC
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.9.4 compiled on May 9 2014
Ivy: non official version
JVM: 1.8.0_40 (Oracle Corporation 25.40-b25)
OS: Linux 3.16.0-31-generic amd64

Additional references:

update-alternatives --display java
java - auto mode
  link currently points to /usr/lib/jvm/java-7-oracle/jre/bin/java
/usr/lib/jvm/java-7-oracle/jre/bin/java - priority 2
  slave java.1.gz: /usr/lib/jvm/java-7-oracle/man/man1/java.1.gz
Current 'best' version is '/usr/lib/jvm/java-7-oracle/jre/bin/java'.

update-alternatives --display javac
javac - auto mode
  link currently points to /usr/lib/jvm/java-7-oracle/bin/javac
/usr/lib/jvm/java-7-oracle/bin/javac - priority 2
  slave javac.1.gz: /usr/lib/jvm/java-7-oracle/man/man1/javac.1.gz
Current 'best' version is '/usr/lib/jvm/java-7-oracle/bin/javac'.

How do I fix this mess? Any help would be most appreciated! Also, if I need to remove the earlier v1.7, how do I do this?

7
  • You need to remove 1.7 altogether.
    – Virusboy
    Mar 9, 2015 at 17:44
  • What are the outputs of update-alternatives --display java and update-alternatives --display javac?
    – heemayl
    Mar 9, 2015 at 17:45
  • I've just added the output for update-alternatives. Mar 9, 2015 at 18:11
  • 1
    Run the command sudo update-alternatives --install /usr/bin/java java /usr/local/java/jdk1.8.0_40/bin/java 100 && sudo update-alternatives --install /usr/bin/javac javac /usr/local/java/jdk1.8.0_40/bin/javac 100 ....then check..(note: use @ infront of my name to tag me in the comment, otherwise i won't get any notification)
    – heemayl
    Mar 9, 2015 at 18:20
  • @heemayl than, this worked like a charm! If you write this as an answer, I can accept it. Another query, when I unpacked the jdk tar file, I only got the jdk folder and not the uncompressed jre folder. Is that okay? Mar 9, 2015 at 19:14

2 Answers 2

3

You should use the alternatives: a way that Ubuntu offers to support multiple versions of a software.

After you've installed the Oracle JDK, tell it to the alternatives:

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.8.0_40/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.8.0_40/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jdk1.8.0_40/bin/javaws" 1

After install, select the default alternatives:

sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config javaws

And then you can select in a list which alternative to use.

WebUpd8 PPA

WebUpd8 is a popular Ubuntu website and they keep a lot of really useful PPAs. Their Java PPA is great, because due the Oracle EULA, Oracle Java cannot be redistributed, but the PPA runs a JDK installer (similar to the flash-installer approach).

This PPA is oftenly updated and has even Java 9. So, you can always keep your Java automatically updated and properly configured.

To add the PPA and install Java8 from there:

sudo add-apt-repository ppa:webupd8team/java 
sudo apt-get update
sudo apt-get install oracle-java8-installer
3
  • The 1 at the end of each line is to mark it as default alternative....this is absolutely wrong..this will set the priority value as 1..if any other has a higher value that one will be set as default..also the locations of the binaries can vary....
    – heemayl
    Mar 9, 2015 at 19:31
  • @heemayl For location, I've used the ones as the author used. For priority, you're right and I'll correct. Mar 9, 2015 at 19:53
  • No, the correct location of the new version's binaries given by OP is /usr/local/java/jdk1.8.0_40/bin..check my answer..
    – heemayl
    Mar 9, 2015 at 19:56
2

Your installation is all right but you need the alternatives system to set the default binary to be executed when you just type jave or javac. Basically the alternatives system lets you to select a binary to run when there are many other alternative binaries having the same functionality.

In your case you have two versions of java installed with alternatives having only the old version linked as generic java binary. You need to add the new version in the java binary alternatives and also set it as default by prioritizing it over others by using a numerical value.

sudo update-alternatives --install /usr/bin/java java /usr/local/java/jdk1.8.0_40/bin/java 100
sudo update-alternatives --install /usr/bin/javac javac /usr/local/java/jdk1.8.0_40/bin/javac 100

This command will do the job, we have linked the new binaries to the java and javac alternatives, also set those as default by giving them higher priority (100) over the older versions (2 - checked by update-alternatives --display java & update-alternatives --display javac, you can also use update-alternatives --query java).

You must log in to answer this question.

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