JAVA_HOME and PATH are set but java -version still shows the old one
Asked Answered
K

10

49

I am using Linux Mint Cinnamon 14. I have set the $JAVA_HOME and $PATH environment variables in ~/.profile as follows:

export JAVA_HOME=/home/aqeel/development/jdk/jdk1.6.0_35
export PATH=/home/aqeel/development/jdk/jdk1.6.0_35/bin:$PATH

I then did source ~/.profile to make the proper changes.

When I execute java -version command to check the active java version, it shows the default (already installed open-jdk) java version. How can I override the default open-jdk with the one I downloaded?

UPDATE:

which java says /usr/bin/java

$JAVA_HOME/bin/java -version says 'Permission Denied'

sudo $JAVA_HOME/bin/java -version (asks for password, then) says Command not found

but cd $JAVA_HOME/bin, and ls shows that it is right directory.

Kurdistan answered 2/1, 2013 at 9:58 Comment(2)
may be reverse the PATH defenition export PATH=$PATH:/home/aqeel/development/jdk/jdk1.6.0_35/binPagoda
if you are still in the same session then you'll have to reload profile source ~/.profilePrivative
Z
71

While it looks like your setup is correct, there are a few things to check:

  1. The output of env - specifically PATH.
  2. command -v java tells you what?
  3. Is there a java executable in $JAVA_HOME\bin and does it have the execute bit set? If not chmod a+x java it.

I trust you have source'd your .profile after adding/changing the JAVA_HOME and PATH?

Also, you can help yourself in future maintenance of your JDK installation by writing this instead:

export JAVA_HOME=/home/aqeel/development/jdk/jdk1.6.0_35
export PATH=$JAVA_HOME/bin:$PATH

Then you only need to update one env variable when you setup the JDK installation.

Finally, you may need to run hash -r to clear the Bash program cache. Other shells may need a similar command.

Cheers,

Zales answered 2/1, 2013 at 10:6 Comment(5)
I see, no file in the $JAVA_HOME/bin directory is marked as executable. Please tell me what other files I have to mark as executable? (Also please see the question I have edited it) Thanks a lotKurdistan
See my bullet 3 above. chmod a+x * in the bin directory will probably do the trick. Also read man chmod and something on permissions on unix/linux in general.Zales
Ok, after doing chmod a+x * in the bin directory, the $JAVA_HOME/bin/java -version outcome is received, but java -version is still the old default one.Kurdistan
Close the shell, open a new one, make sure the PATH is set correctly, try again.Zales
When exporting the PATH, place the $PATH itself as last parameter. Otherwise you will probably still reference the old one.Sizzle
E
21

update-java-alternatives

The java executable is not found with your JAVA_HOME, it only depends on your PATH.

update-java-alternatives is a good way to manage it for the entire system is through:

update-java-alternatives -l

Sample output:

java-7-oracle 1 /usr/lib/jvm/java-7-oracle
java-8-oracle 2 /usr/lib/jvm/java-8-oracle

Choose one of the alternatives:

sudo update-java-alternatives -s java-7-oracle

Like update-alternatives, it works through symlink management. The advantage is that is manages symlinks to all the Java utilities at once: javac, java, javap, etc.

I am yet to see a JAVA_HOME effect on the JDK. So far, I have only seen it used in third-party tools, e.g. Maven.

Elysha answered 1/4, 2015 at 6:45 Comment(1)
update-alternative command not found helpedMouser
K
12

If you want to use JDKs downloaded from Oracle's site, what worked for me (using Mint) is using update-alternatives:

  1. I downloaded the JDK and extracted it just anywhere, for example in /home/aqeel/development/jdk/jdk1.6.0_35
  2. I ran:

    sudo update-alternatives --install /usr/bin/java java /home/aqeel/development/jdk/jdk1.6.0_35/bin/java 1
    

    Now you can execute sudo update-alternatives --config java and choose your java version.

  3. This doesn't set the JAVA_HOME variable, which I wanted configured, so I just added it to my ~/.bashrc, including an export JAVA_HOME="/home/aqeel/development/jdk/jdk1.6.0_35" statement

Now, I had two JDKs downloaded (let's say the second has been extracted to /home/aqeel/development/jdk/jdk-10.0.1).

How can we change the JAVA_HOME dynamically based on the current java being used?

My solution is not very elegant, I'm pretty sure there are better options out there, but anyway:

  1. To change the JAVA_HOME dynamically based on the chosen java alternative, I added this snippet to the ~/.bashrc:

    export JAVA_HOME=$(update-alternatives --query java | grep Value: | awk -F'Value: ' '{print $2}' | awk -F'/bin/java' '{print $1}')
    

Finally (this is out of the scope) if you have to change the java version constantly, you might want to consider:

  1. Adding an alias to your ~./bash_aliases:

    alias change-java="sudo update-alternatives --config java"
    

(You might have to create the file and maybe uncomment the section related to this in ~/.bashrc)

Kocher answered 8/6, 2018 at 18:55 Comment(3)
It seems the last character should be removed, no? sudo update-alternatives --install /usr/bin/java java /home/aqeel/development/jdk/jdk1.6.0_35/bin/java 1`Firestone
right, the command is sudo update-alternatives --install /usr/bin/java java /home/aqeel/development/jdk/jdk1.6.0_35/bin/java 1, just fixed that thanks for the heads up @CleberJorgeAmaralKocher
The only correct answer on the InternetDelegate
H
4

$JAVA_HOME/bin/java -version says 'Permission Denied'

If you cannot access or run code, it which be ignored if added to your path. You need to make it accessible and runnable or get a copy of your own.

Do an

ls -ld $JAVA_HOME $JAVA_HOME/bin $JAVA_HOME/bin/java

to see why you cannot access or run this program,.

Hermon answered 2/1, 2013 at 10:25 Comment(2)
Ooops, It was not marked as executable (as @Anders R. Bystrup pointed out in his answer). After doing chmod a+x * in the bin directory, the $JAVA_HOME/bin/java -version outcome is received, but java -version is still the old default one.Kurdistan
try calling hash -r to cause the shell remove it's remembered locations.Hermon
E
4

When it searches for java it looks from left to right in path entries which are separated by : so you need to add the path of latest jdk/bin directory before /usr/bin, so when it searches it'll find the latest one and stop searching further.

i.e. PATH=/usr/java/jdk_1.8/bin:/usr/bin:..... and so on.

then initialize user profile using command: source ~/.bash_profile

and check with: [which java]

you'll get the right one.

Essam answered 25/5, 2014 at 12:10 Comment(0)
A
3

Try this:

  • export JAVA_HOME=put_here_your_java_home_path
  • type export PATH=$JAVA_HOME/bin:$PATH (ensure that $JAVA_HOME is the first element in PATH)
  • try java -version

Reason: there could be other PATH elements point to alternative java home. If you put first your preferred JAVA_HOME, the system will use this one.

Aristophanes answered 6/6, 2019 at 17:5 Comment(1)
ok, this should be accepted as the correct solution, instead of using 3rd party services like sdk and all this is great, that $JAVA_HOME part in the PATH variable is exactly what I have been missing.Jabot
L
2

check available Java versions on your Linux system by using update-alternatives command:

 $ sudo update-alternatives --display java

Now that there are suitable candidates to change to, you can switch the default Java version among available Java JREs by running the following command:

 $ sudo update-alternatives --config java

When prompted, select the Java version you would like to use.1 or 2 or 3 or etc..

Now you can verify the default Java version changed as follows.

 $ java -version
Lemcke answered 24/1, 2019 at 5:59 Comment(0)
U
1

There is an easy way, just remove the symbolic link from "/usr/bin". It will work.

Univalent answered 20/11, 2013 at 12:18 Comment(0)
B
1

Updating the ~/.profile or ~/.bash_profile does not work sometimes. I just deleted JDK 6 and sourced .bash_profile.

Try running this:

sudo rm -rd jdk1.6.0_* #it may not let you delete without sudo

Then, modify/add your JAVA_HOME and PATH variables.

source ~/.bash_profile #assuming you've updated $JAVA_HOME and $PATH
Buddleia answered 13/7, 2015 at 18:13 Comment(0)
R
0

In Linux Mint 18 Cinnamon be sure to check /etc/profile.d/jdk_home.sh I renamed this file to jdk_home.sh.old and now my path does not keep getting overridden and I can call java -version and see Java 9 as expected. Even though I correctly selected Java 9 in update-aternatives --config java this jdk_home.sh file kept overriding the $PATH on boot-up.

Rutger answered 9/12, 2017 at 20:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.