How to set JAVA_HOME in Linux for all users
I am new to Linux system and there seem to be too many Java folders.
java -version gives me:
- java version "1.7.0_55"
- OpenJDK Runtime Environment (rhel-2.4.7.1.el6_5-x86_64 u55-b13)
- OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)
When I am trying to build a Maven project , I am getting error:
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/java/jdk1.7.0_05/bin/java
Could you please tell me which files I need to modify for root as well as not-root user and where exactly is java located?
Solution 1:
find /usr/lib/jvm/java-1.x.x-openjdk
-
vim /etc/profile
Prepend sudo if logged in as not-privileged user, ie.
sudo vim
- Press 'i' to get in insert mode
-
add:
export JAVA_HOME="path that you found" export PATH=$JAVA_HOME/bin:$PATH
- logout and login again, reboot, or use
source /etc/profile
to apply changes immediately in your current shell
Solution 2:
For all users, I would recommend creating a file in /etc/profile.d/java_home.sh
the following lines
# Set JDK installation directory according to selected Java compiler
export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
This will update dynamically and works well with the alternatives system. Do note though that the update will only take place in a new login shell.