Where the JAVA_HOME variable should be set for Hadoop ?

If JAVA_HOME is not properly set, you may see error like this:

Error: JAVA_HOME is not set and could not be found

Generally speaking, there are two places to set Hadoop environment variables:

 one is the ~.bashrc
the other is etc/hadoop/hadoop-env.sh

You can set most of Hadoop management environment variables into .bashrc, for example:

export HADOOP_HOME=/opt/hadoop
export HADOOP_INSTALL=$HADOOP_HOME
export HADOOP_MAPRED_HOME=$HADOOP_HOME
export HADOOP_COMMON_HOME=$HADOOP_HOME
export HADOOP_HDFS_HOME=$HADOOP_HOME
export YARN_HOME=$HADOOP_HOME
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
export PATH=$PATH:$HADOOP_HOME/sbin:$HADOOP_HOME/bin

However, for JAVA_HOME, it has to be set in etc/hadoop/hadoop-env.sh, it has default setting

export JAVA_HOME=${JAVA_HOME}

You should run the following commands to identify the JAVA_HOME on your server

 $which java
/bin/java
$ ls -l /bin/java
lrwxrwxrwx 1 root root 22 Jul 29 08:48 /bin/java -> /etc/alternatives/java
$ ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 45 Jul 29 08:48 /etc/alternatives/java -> /usr/lib/jvm/jre-1.8.0-oracle.x86_64/bin/java

Then append the line below to etc/hadoop/hadoop-env.sh

JAVA_HOME=/usr/lib/jvm/jre-1.8.0-oracle.x86_64/