My goal is to set the environment variable
JAVA_HOMEfor an user named
gridwhich is just the traditional user name for hadoop.
My machine is a virtual machine deployed in VMware Workstation, 64bit CentOS7.
What I do: I edit the file that is supposed to change my user-specific environment variable, i.e.
~/.bash_profile. Below is my code:
export JAVA_HOME=/home/grid/jdk export HADOOP_HOME=/home/grid/hadoop PATH=$PATH:$HADOOP_HOME:$JAVA_HOME export PATH
Very interestingly, when I log in as user
grid, and I run
echo $HADOOP_HOME, I get
/home/grid/hadoop, but I get an nothing as response when I run
echo $JAVA_HOME, literally just nothing, an empty string or null, something like that. I run
cd $JAVA_HOME, and I end up in home directory.
I tried changing the JDK folder, it didn't work. I tried the same code on another machine, it worked. I put the code in
/etc/profileand log in as root, same thing happened, good for
echo $HADOOP_HOMEbut no response for
First, an item of note on
$PATH. You have the
JAVA_HOME set to the
jdk directory, but for the
PATH, you need to make that
Second, if you need these variables to be available to non-interactive scripts, you may want to put them in
~/.bash_profile is read by interactive logins only.
In fact, the actual order in which (login shell) profiles are read is:
~/.bash_profile(On CentOS, if
-f ~/.bashrc, sources
~/.profile(On Ubuntu, if
-n $BASH_VERSION, sources
EDIT: If one of these files is found, further files are not processed unless explicitly sourced
Non-login shells (by default) only look for
So, depending on how you're executing
bash, you may not even be reading the profile you've added these lines to. Are you executing
sudo -u grid bash, or
su grid, or
su - grid, or initiating a new ssh session each time you test? Some of those options may not trigger an interactive login bash shell.
So check each of the files listed above, and see if any of them are unsetting, or resetting the
JAVA_HOME variable. Then try adding the lines to the end of
~/.profile, which is the last file sourced by a login shell.
For debugging purposes you can add messages to the profile to make sure it's getting set right, too.
export JAVA_HOME=/home/grid/jdk echo "DEBUG: .profile: JAVA_HOME is [$JAVA_HOME]" export HADOOP_HOME=/home/grid/hadoop echo "DEBUG: .profile: HADOOP_HOME is [$HADOOP_HOME]" PATH=$HADOOP_HOME:$JAVA_HOME/bin:$PATH echo "DEBUG: .profile: PATH is [$PATH]" export PATH
This will cause 3 lines of output when you login to an interactive login shell, and the values between the brackets will make it easy to see if the values are being set to begin with. If you see that
JAVA_HOME is being set when
~/.profile is sourced, but is not set when the shell becomes interactive, then you know you need to keep following the login process to determine where the value is changing.