How to Set JAVA_HOME / PATH variables Under Linux Bash Profile

I just need a help to show me how to setup java path on Linux. How can I set JAVA_HOME and PATH variables for every user under my Linux system?

~/.bash_profile is a startup script which generally runs once. This particular file is used for commands which run when the normal user logs in. Common uses for .bash_profile are to set environment variables such as PATH, JAVA_HOME, to create aliases for shell commands, and to set the default permissions for newly created files.

Set JAVA_HOME / PATH for a single user

Login to your account and open .bash_profile file
$ vi ~/.bash_profile
Set JAVA_HOME as follows using syntax export JAVA_HOME=<path-to-java>. If your path is set to /usr/java/jdk1.5.0_07/bin/java, set it as follows:
export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
Set PATH as follows:
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
Feel free to replace /usr/java/jdk1.5.0_07 as per your setup. Save and close the file. Just logout and login back to see new changes. Alternatively, type the following command to activate the new path settings immediately:
$ source ~/.bash_profile
$ . ~/.bash_profile
Verify new settings:
$ echo $JAVA_HOME
$ echo $PATH

Tip: Use the following command to find out exact path to which java executable under UNIX / Linux:
$ which java

Please note that the file ~/.bashrc is similar, with the exception that ~/.bash_profile runs only for Bash login shells and .bashrc runs for every new Bash shell.

Set JAVA_HOME / PATH for all user

You need to setup global config in /etc/profile OR /etc/bash.bashrc file for all users:
# vi /etc/profile
Next setup PATH / JAVA_PATH variables as follows:
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin

Save and close the file. Once again you need to type the following command to activate the path settings immediately:
# source /etc/profile
# . /etc/profile

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

88 comments… add one
  • Macronic Titanic Jan 21, 2015 @ 12:49

    Any use of a static path is poor choice from the maintenance standpoint: it will have to be changed each time java updates which is often (if one is security-focused.

    I suggest this aproach:

    1. Use /etc/alternatives to always have the current version of java at /usr/bin/java
    2. Set JAVA_HOME in /etc/profile.d/

    export JAVA_HOME=$(readlink -e /usr/bin/java | sed ‘s/\(.*\)bin\/java$/\1/’)

  • Ajesh May 7, 2015 @ 8:49

    Well done Mustafa ….. well worked…..

  • roshan May 28, 2015 @ 5:07

    configure: error: failed to determine linker flags for using Java

    (bad JAVA_HOME or missing support for your architecture?)
    How to solve above issue

  • pavankumar Jul 2, 2015 @ 6:50

    i want set environment variables in centOS like

    variable Name: JAVA_TOOL_OPTIONS

    value : -Dfile.encoding=UTF-8

    these are my environment variables

    i want set permanently

    but i already tried like
    export JAVA_TOOL_OPTIONS=”-Dfile.encoding=UTF-8 ” in bash file .
    but even a reboot the our system also show same output.

    plz tell me how to resolve it.

  • Craig Schafer Jul 9, 2015 @ 0:41

    I figured out the solution but the comments here did not help. The basic problem being faced is the other version of java is found in /usr/bin/ and there is a path already to /usr/bin therefore that version was being used rather than the new version. When I typed “which java” is would always show “/usr/bin/java”. Yes JAVA_HOME needs to be set correctly to be added to the path; however the wrong path was used. I just went to /usr/bin/ and move java to java_old. After I logged in and out “java -version” always showed the correct version and worked.

  • Bunts Aug 24, 2016 @ 16:15

    The JAVA_HOME points to the top level directory of the JDK/JRE. You have it wrong.

  • Madhusudan Jan 10, 2017 @ 13:00

    Very helpful material…

  • Mohsen Pahlevanzadeh Aug 20, 2017 @ 20:32

    JAVA_HOME=$(readlink -f /usr/bin/java | sed “s:bin/java::”)

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.