JAVA_HOME and PATH are set but java -version still shows the old one
Solution 1:
While it looks like your setup is correct, there are a few things to check:
- The output of
env
- specificallyPATH
. -
command -v java
tells you what? - Is there a
java
executable in$JAVA_HOME\bin
and does it have the execute bit set? If notchmod 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,
Solution 2:
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.
Solution 3:
If you want to use JDKs downloaded from Oracle's site, what worked for me (using Mint) is using update-alternatives:
- I downloaded the JDK and extracted it just anywhere, for example in /home/aqeel/development/jdk/jdk1.6.0_35
-
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. - 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:
-
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:
-
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)
Solution 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,.