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

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
88 comments… add one
  • ZehcnasAlex May 22, 2012 @ 15:35

    Hi, maybe if you’ll try with apt-get install maven2, i did it in this way and still i dont have any problem.

  • yaseen May 31, 2012 @ 8:23

    Dear Guru,
    I am new to sap.please send me document hot to install gateway instance using xmanager.please help me to this os is Linux susi 11 and database is oracel


  • eb Jul 16, 2012 @ 2:45

    This article is stupidly wrong and user spring above is correct.
    First find out where your jdk is installed. Then set as below:
    export JAVA_HOME=/usr/java/jdk1.6.0_18

    After doing this way my Tomcat install worked fine.

  • Gajesh Sep 14, 2012 @ 12:35

    Hi I have installed cygwin for windows xp, can you please tell me how to set JAVA_HOME variable? its not taking the windows path.

  • seighalani Dec 16, 2012 @ 20:59


    thanks for your good article.
    please correct end of article. there is duplicate phrase . i mean

    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

  • Anugya Feb 26, 2013 @ 12:49

    Thank It help me lot!!!!!!!!!!!!!!!!

  • sanjib deka Apr 16, 2013 @ 9:30

    I have installed jdk1.7.0.17. when i check java version and path it shows
    OpenJDK version and the path like

    How can change it to :

    java version “1.7.0_17″
    Java(TM) SE Runtime Environment
    Java HotSpot(TM) Server VM

    kindly help me please.

  • ash7 Jul 3, 2013 @ 5:33

    To summarise and simplify the procedure. The following would set the env variables for the particular user.
    Step 1. Open the ~/.bashrc file
    nano /home/user/.bashrc

    Step 2: Set the environment variables as follows. Just put these on the top of the file and save it.
    # user defined environment variables
    # ———————————-
    export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-amd64
    export PATH=$PATH:/$JAVA_HOME/bin

    Step 3. Logout and login again.

    Step 4. Open terminal and type echo $JAVA_HOME and echo $PATH
    this should return you the appropriate result as you had set.

    Note: I have my java installed at /usr/lib/jvm/java-1.6.0-openjdk-amd64 location. It may vary in your system.

    • Atul Sep 10, 2013 @ 17:34

      Thanks ash7 ! it’s Working..

    • Kiss Sep 11, 2013 @ 10:49

      I searched “.bashrc” in file system and
      i found 2 files bash.bashrc and 2 files dot.bashrc how can i choose the right one?
      in my home/user/ there is no file .bashrc
      please help me!

    • sam ben Mar 27, 2014 @ 19:29

      yeah it worked for me .. thanks man

  • Atul Sep 10, 2013 @ 17:29

    System Got stuck!!!

  • saji Nov 6, 2013 @ 11:14

    i have problem for running selenium script in linux machine i have received this error

    kindly help me out or pls give me messge ill cal clarify… error mention below is not on the classpath.
    Perhaps JAVA_HOME does not point to the JDK.
    It is currently set to “/usr/lib/jvm/java-7-openjdk-i386/jre”

  • 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>, <code>...</code> and <kbd>...</kbd> for code samples.