How to set JAVA_HOME in Linux for all users
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.
Solution 3
You could use /etc/profile or better a file like /etc/profile.d/jdk_home.sh
export JAVA_HOME=/usr/java/jdk1.7.0_05/
You have to remember that this file is only loaded with new login shells.. So after bash -l or a new gnome-session and that it doesn't change with new Java versions.
Solution 4
None of the other answers were "sticking" for me in RHEL 7, even setting JAVA_HOME
and PATH
directly in /etc/profile
or ~/.bash_profile
would not work. Each time I tried to check if JAVA_HOME
was set, it would come up blank:
$ echo $JAVA_HOME
(<-- no output)
What I had to do was set up a script in /etc/profile.d/jdk_home.sh
:
#!/bin/sh
export JAVA_HOME=/opt/ibm/java-x86_64-60/
export PATH=$JAVA_HOME/bin:$PATH
I initially neglected the first line (the #!/bin/sh
), and it won't work without it.
Now it's working:
$ echo $JAVA_HOME
/opt/ibm/java-x86_64-60/
Solution 5
Open terminal and type
sudo gedit .bashrc
It will ask you your password. After typing the password, it will open the bash file. Then go to end and type:
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/" export PATH=$PATH:$JAVA_HOME/bin
Then save the file and exit from file
Above is for a single user. For all users, you have to follow below steps
gedit /etc/profile
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"
export PATH=$PATH:$JAVA_HOME/bin
Related videos on Youtube
Bosco
Updated on March 15, 2022Comments
-
Bosco about 2 years
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?
-
Sunil Kumar over 9 yearsdon't forget to delete the double quotes and recreate them from your keyboard, because only copying and pasting may create troubles.
-
rbaleksandar about 9 yearsI do believe you can skip the "export JAVA_HOME" part and simply do an "export PATH=$PATH:/path_to_java/bin". I haven't had a problem so far when not defining JAVA_HOME but maybe it's just pure luck on my part.
-
raffian almost 9 years@rbaleksandar some applications depend on
JAVA_HOME
, doesn't hurt setting it up too. -
rbaleksandar almost 9 yearsMaybe, never encountered such an application.
-
Pieter van den Ham almost 9 yearsIntelliJ is such an application - and not a minor one.
-
Buurman over 8 yearsIt seems a potentially correct answer but could you explain why it works, ie what it does and what the OP's problem is? Also, you say "on Linux", but there are many different Linux distro's and it might not work for all of them, please add for which distro this works.
-
Mohamed Taher Alrefaie almost 8 yearsYou need to run
source /etc/profile
for changes to take effect immediately too! -
Manuel Manhart over 7 yearsthis answer sets it only for the current user.
-
Roman Kruglov about 7 yearsto comply with just plain JRE (even headless) use
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")
instead - notice that I use justjava
, notjavac
-
Eero Aaltonen about 7 years@occulta At least Maven expects JAVA_HOME to point to a JDK and uses it to locate the java compiler.
-
Hilikus almost 7 yearsat least in my linux (raspbian), /etc/profile will source
/etc/profile.d/*.sh
so your file needs to be calledjdk_home.sh
so it gets sourced -
flob almost 7 yearsThanks @Hilikus :-) I changed it accordingly.
-
Hilikus almost 7 yearsThank you. i did a mix of your and @Eero's answer for the best of both worlds ;)
-
G_V over 6 yearsprintenv JAVA_HOME to check the result afterward
-
Brent Sandstrom about 6 yearsMake sure you don't already have these options in the file before you add them.
-
Rajesh Mbm about 6 yearsthis is the right solution. In the next reboot changes picked automatically as expected. if you do the same changes in /etc/environment then changes will be for current session only. /etc/profile is right place to update paths permanently.
-
8bitjunkie almost 6 yearsThis doesn't work for my tomcat user. It can't resolve
$JAVA_HOME
in its/etc/init.d
startup script when set in this way. -
xpagesbeast almost 6 yearsI had the same experience on RHEL 7. I removed the exports from the ~/.bash_profile and used this approach.
-
RocketRuwan over 5 yearsI think it is safe to mention how to save the updates you make by going into command mode by Pressing "Esc" and then typing ":w" and pressing "Enter".
-
ChristophS almost 5 yearsUpvote for
readlink
and update/upgrade compatible solution, even this is an 5 years old thread. I only recommend not to edit /etc/profile, but place your export inside custom file, e.g./etc/profile.d/java_environment.sh
, maybe you have tochmod +x java_environment.sh
and reboot. -
Ungeheuer over 4 yearsPerfect. Better than my clunky
dirname
-ing solution. If you're like me and wanting to understand what's going on here, this is a simple replacement of matching text from input with an empty string. The "default" character for replacements is the/
, but as long as you're consistent, you can replace the/
with anything. In this case it's colons as we use/
for path separators. -
kuhajeyan over 4 years#!/bin/sh is not required in your jdk_home.sh. once you done the configuration make sure to logout and login again
-
Ng Sek Long over 4 years
/etc/profile.d/jdk_home.sh
is the much more clean answer, at least for Ubuntu ,/etc/profile
is clumped with so much logic already. Didn't seems wise to add more onto it... -
Fatih Türker over 4 yearsolny effects current terminal session
-
Hermann over 4 yearsThis pointed me to the right direction. There actually was a stale
/etc/profile.d/jdk.sh
floating around in my system. -
Jack G almost 4 years@FatihTürker Reboot your PC
-
Jack G almost 4 yearsFor me using jdk11, YOUR_PATH is
/usr/lib/jvm/default-java/
-
Gilbert almost 4 yearsThe JAVA_HOME path is set but it doesnt work. ie java doesnt work. Even after reboot
-
Talha Asghar over 2 yearsThis method does not works if you are trying to make java available to a systemd service,
systemctl start <your_application>
. For that UseEnviornment="JAVA_HOME=path_to_java"
in your.service
file. -
leonidaa about 2 yearsyou can use
update-java-alternatives -l
shows you all the Java versions you have installed. alternative to thisfind /usr/lib/jvm/java-1.x.x-openjdk
-
PyDevSRS about 2 years@AdamHughes this is for unix system. not sure what system you are talking about
-
Alkanshel about 2 yearsAlso, using the default jvm symlink for java_home is probably good stackoverflow.com/questions/663658/…