How to set JAVA_HOME in Linux for all users

1,103,859

Solution 1

  1. find /usr/lib/jvm/java-1.x.x-openjdk
  2. vim /etc/profile

    Prepend sudo if logged in as not-privileged user, ie. sudo vim

  3. Press 'i' to get in insert mode
  4. add:

    export JAVA_HOME="path that you found"
    
    export PATH=$JAVA_HOME/bin:$PATH
    
  5. 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

  1. Open terminal and type sudo gedit .bashrc

  2. 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
    
  3. Then save the file and exit from file

Above is for a single user. For all users, you have to follow below steps

  1. gedit /etc/profile

  2. export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"

  3. export PATH=$PATH:$JAVA_HOME/bin

Share:
1,103,859

Related videos on Youtube

Bosco
Author by

Bosco

Updated on March 15, 2022

Comments

  • Bosco
    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
    Sunil Kumar over 9 years
    don't forget to delete the double quotes and recreate them from your keyboard, because only copying and pasting may create troubles.
  • rbaleksandar
    rbaleksandar about 9 years
    I 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
    raffian almost 9 years
    @rbaleksandar some applications depend on JAVA_HOME, doesn't hurt setting it up too.
  • rbaleksandar
    rbaleksandar almost 9 years
    Maybe, never encountered such an application.
  • Pieter van den Ham
    Pieter van den Ham almost 9 years
    IntelliJ is such an application - and not a minor one.
  • Buurman
    Buurman over 8 years
    It 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
    Mohamed Taher Alrefaie almost 8 years
    You need to run source /etc/profile for changes to take effect immediately too!
  • Manuel Manhart
    Manuel Manhart over 7 years
    this answer sets it only for the current user.
  • Roman Kruglov
    Roman Kruglov about 7 years
    to 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 just java, not javac
  • Eero Aaltonen
    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
    Hilikus almost 7 years
    at least in my linux (raspbian), /etc/profile will source /etc/profile.d/*.sh so your file needs to be called jdk_home.sh so it gets sourced
  • flob
    flob almost 7 years
    Thanks @Hilikus :-) I changed it accordingly.
  • Hilikus
    Hilikus almost 7 years
    Thank you. i did a mix of your and @Eero's answer for the best of both worlds ;)
  • G_V
    G_V over 6 years
    printenv JAVA_HOME to check the result afterward
  • Brent Sandstrom
    Brent Sandstrom about 6 years
    Make sure you don't already have these options in the file before you add them.
  • Rajesh Mbm
    Rajesh Mbm about 6 years
    this 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
    8bitjunkie almost 6 years
    This 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
    xpagesbeast almost 6 years
    I had the same experience on RHEL 7. I removed the exports from the ~/.bash_profile and used this approach.
  • RocketRuwan
    RocketRuwan over 5 years
    I 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
    ChristophS almost 5 years
    Upvote 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 to chmod +x java_environment.sh and reboot.
  • Ungeheuer
    Ungeheuer over 4 years
    Perfect. 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
    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
    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
    Fatih Türker over 4 years
    olny effects current terminal session
  • Hermann
    Hermann over 4 years
    This pointed me to the right direction. There actually was a stale /etc/profile.d/jdk.sh floating around in my system.
  • Jack G
    Jack G almost 4 years
    @FatihTürker Reboot your PC
  • Jack G
    Jack G almost 4 years
    For me using jdk11, YOUR_PATH is /usr/lib/jvm/default-java/
  • Gilbert
    Gilbert almost 4 years
    The JAVA_HOME path is set but it doesnt work. ie java doesnt work. Even after reboot
  • Talha Asghar
    Talha Asghar over 2 years
    This method does not works if you are trying to make java available to a systemd service, systemctl start <your_application>. For that Use Enviornment="JAVA_HOME=path_to_java" in your .service file.
  • leonidaa
    leonidaa about 2 years
    you can useupdate-java-alternatives -l shows you all the Java versions you have installed. alternative to this find /usr/lib/jvm/java-1.x.x-openjdk
  • PyDevSRS
    PyDevSRS about 2 years
    @AdamHughes this is for unix system. not sure what system you are talking about
  • Alkanshel
    Alkanshel about 2 years
    Also, using the default jvm symlink for java_home is probably good stackoverflow.com/questions/663658/…