2

I am trying to set JAVA_HOME in Ubuntu OS. I have copied jdk 1.7 in /usr/lib/jvm and set JAVA_HOME in /etc/profile file.

Contents of /usr/lib/jvm folder are as follows :

shekhar@ubuntu:~$ ls /usr/lib/jvm/
default-java        java-1.6.0-openjdk       java-6-openjdk         java-6-openjdk-i386  jdk1.7.0_01
java-1.5.0-gcj-4.6  java-1.6.0-openjdk-i386  java-6-openjdk-common  java-7-openjdk-i386

and last few lines of /etc/profile file are as follows :

export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_01
export PATH=$PATH:$JAVA_HOME/bin

After finishing all this when I run java -version command I get following output :

shekhar@ubuntu:~$ java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.4) (6b24-1.11.4-1ubuntu0.12.04.1)
OpenJDK Server VM (build 20.0-b12, mixed mode)

and when I run ls -lah command I get following output :

shekhar@ubuntu:~$ ls -lah /usr/bin/java
lrwxrwxrwx 1 root root 22 Sep 29 09:58 /usr/bin/java -> /etc/alternatives/java
shekhar@ubuntu:~$ ls -lah /etc/alternatives/java
lrwxrwxrwx 1 root root 45 Sep 29 09:58 /etc/alternatives/java -> /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java

Can anyone please tell me which thing I am missing? Why Ubuntu is still pointing to open jdk and not to my jdk 7?

PS : I have seen this similar question and its answers but that question is related to Windows OS and not for Ubuntu so I am reposting this similar question for Ubuntu.

3 Answers 3

4

You set the JAVA_HOME environment variable, and then you used that to set your PATH. However, the path you set also consists of the existing path. Essentially, you just added something else to your existing path.

The path to your JDK1.7 is added at the end of the path. When you type 'java --version' on the command line, the system searches through the path from beginning to end until it finds a path that has the command 'java'. Therefore, what you're seeing is the version output from one of your other Java versions.

What I typically do on Ubuntu is look at my path:

echo $PATH

and then look for possible JVM paths near the beginning. At that point, you'd have several options to fix this:

  1. Create a symlink pointing the system to your new Java path.

  2. Remove the old Java version and remove it from your path.

I'd opt for option 2. It's less confusing to just get rid of what you don't need.

1
  • 1
    Hi, You were right. Somehow there's other open jdk installed which was coming before my jdk 1.7. I just re arranged PATH variable so as to bring my jdk first in PATH. Now its working fine. Thanks...
    – Shekhar
    Sep 29, 2012 at 14:44
2

I personally was facing exactly the same kind of problem on a fresh Ubuntu 12.04 install. All I had to do is to uninstall a specific package using the following command:

sudo apt-get remove openjdk-6-jre-headless

This removed the link in the /etc/alternatives and updated it to the installed java 1.7

Just wanted to share as this completes point 2 in jmort253's answer.

2

I had the same problem, and the answer is hidden in the PATH:

running "$ env | grep java" shows:

PATH=/u01/app/oracle/product/11.2.0/xe/bin:/home/rivaldo/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/java/jdk1.6.0_14/bin:/home/rivaldo/bin/eclipse.JEE.Kepler.64
JAVA_HOME=/usr/local/java/jdk1.6.0_14
JDK_HOME=/usr/local/java/jdk1.6.0_14

but running "java -version" still show:

java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)

I figured out that my PATH has "/usr/bin" before "/java/jdk1.6.0_14/bin" and doing "ll /usr/bin | grep java" i got:

lrwxrwxrwx 1 root root 39 Jul 31 08:29 /etc/alternatives/java -> /usr/lib/jvm/java-7-oracle/jre/bin/java*

I changed the simbolic link to the correct one and problem solved. You may also change the order at path:

from

export PATH=$PATH:$JAVA_HOME/bin

to

export PATH=$JAVA_HOME/bin:$PATH
2
  • My problem solved by your last suggestion. change the order of assignment to PATH. But why it is important? how is this solved? Would you please describe this? May 25, 2020 at 17:38
  • @MukitChowdhury, the command change the order where the OS will look for the java executable (the actual java program), the last command "export PATH=$JAVA_HOME/bin:$PATH" get first the version defined by JAVA_HOME, so the issue is solved because you will use the java defined at "JAVA_HOME" as default. Jul 6, 2020 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.