54

I'm trying to install Hadoop on Ubuntu 11.10. I set the JAVA_HOME variable in the file conf/hadoop-env.sh to:

# export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk

and then I execute these commands (Standalone Operation):

$ mkdir input 
$ cp conf/*.xml input 
$ bin/hadoop jar hadoop-examples-*.jar grep input output 'dfs[a-z.]+' 
$ cat output/*

but I have the following error when executing the third command:

ERROR : JAVA_HOME is not set

Is the JAVA_HOME variable not set correctly?

koukou
  • 613
  • 1
  • 7
  • 13

12 Answers12

64

Make sure that you have removed the comment tag and changed your JAVA_HOME in the hadoop-env.sh as well as the appropriate .bashrc and/or .profile:

# export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk

should be

export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk

You can set your JAVA_HOME and PATH for all users (make sure you haven't previously set this to the wrong path) in /etc/profile.

Also, don't forget to activate the new change by logging-out/in or by executing source /etc/profile.

user1489515
  • 641
  • 5
  • 2
37

You should set JAVA_HOME in the hadoop-env.sh file also which is in the Hadoop configuration directory. By default the JAVA_HOME setting line is commented.

nantitv
  • 3,539
  • 4
  • 38
  • 61
  • 1
    In the current version 2.9.0 it is present but takes the current environment's JAVA_HOME forward. The doco there suggests that this value should be hardcoded for distributed setups. I found that in single node pseudodistributed operation it helps to have it set here. YMMV. – russellpierce Dec 08 '17 at 05:18
  • Yes @russellpierce, hardcoding was actually the only thing working... Whyever, but setting /etc/environemnt, /etc/profile and .bashrc did not help at all – Markus Jul 29 '20 at 00:37
  • For those which couldn't find the hadoop-env.sh file, it can be found using find hadoop/ -name hadoop-env.sh command. In my computer the file was at .../hadoop/etc/hadoop/hadoop-env.sh. – Guilherme Noronha Jan 12 '21 at 16:02
14

Type echo $JAVA_HOME in your terminal to be sure your JAVA_HOME is set.

You can also type java -version to know what version of java you are actually using.

By the way, reading your description it seems your actually writing

export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk

in the file conf/hadoop-env.sh, you should write it in your terminal or in ~/.bashrc or ~/.profile then type source < path to modified file >.

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
  • 1
    yes, the # is by default there so I delete it and I tried these 4 commands again and it works. but when I type "echo $JAVA_HOME" , I don't obtain it value and when I type "java -version" I obtain "java version "1.6.0_23" . Is it correct? OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10) OpenJDK Server VM (build 20.0-b11, mixed mode)" . Is it correct? – koukou Jan 12 '12 at 02:06
  • 3
    If nothing appears writting echo $JAVA_HOME", means that the environnement variable is not properly set. Type sudo vi ~/.bashrc then add in the file : export JAVA_HOME=< java_path > export PATH=$PATH:< java_path >/bin. Then type sudo source ~/.bashrc – alain.janinm Jan 12 '12 at 08:30
  • 1
    I'm following this tutorial http://hadoop.apache.org/common/docs/stable/single_node_setup.html#Download : the JAVA_HOME is defined only in the conf/hadoop-env.sh .I added export PATH=$PATH:< java_path >/bin in the hadoop-env.sh file,typing "echo $PATH " the path appears but typing "echo $ JAVA_HOME" a space appears!!! – koukou Jan 12 '12 at 11:06
  • @alain.janinm - your link is broken – Jeremy Hajek Dec 02 '13 at 05:13
  • @JeremyHajek Thanks for noticing, it often happens after some time... Here is the new link : http://www.michael-noll.com/tutorials/running-hadoop-on-ubuntu-linux-single-node-cluster/ – alain.janinm Dec 03 '13 at 07:35
  • @alain.janinm. Could u just tell me what is the importance of mentioning Java location in .bashrc and hadoop-env.sh. What would happen in the background ? Will the hadoop framework take all the jar files of java for starting hadoop deamons.? – Jon Andrews Apr 18 '17 at 11:42
  • @BasilPaul It's not really about hadoop but abut how java is called (there is a command calling java or javac in the process mentionned by the op). So at some point JAVA_HOME is seeked and you have to define that in your environment. If you don't, your os can't know where the java bin are located and can't run the command. – alain.janinm Apr 18 '17 at 13:12
  • @alain.janinm Thank You – Jon Andrews Apr 19 '17 at 04:26
  • @alain.janinm. What is the real purpose of giving hadoop location in bashrc to variables HADOOP_MAPRED_HOME,HADOOP_COMMON_HOME, HADOOP_HDFS_HOME,YARN_HOME,HADOOP_CONF_DIR,YARN_CONF_DIR – Jon Andrews Apr 19 '17 at 05:21
  • @BasilPaul I'm not sure to be able to answer this question, sorry. You probably will have better chance to obtain a good answer by asking it in a new thread ;) – alain.janinm Apr 19 '17 at 07:23
  • @alain.janinm Okay – Jon Andrews Apr 19 '17 at 11:24
9

You can add in your .bashrc file:

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

and it will dynamically change when you update your packages.

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
2

The solution that worked for me was setting my JAVA_HOME in /etc/environment

Though JAVA_HOME can be set inside the /etc/profile files, the preferred location for JAVA_HOME or any system variable is /etc/environment.

Open /etc/environment in any text editor like nano or vim and add the following line:

JAVA_HOME="/usr/lib/jvm/your_java_directory"

Load the variables:

source /etc/environment

Check if the variable loaded correctly:

echo $JAVA_HOME
Muthukrishnan
  • 2,047
  • 2
  • 16
  • 16
1

Copy this export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk to hadoop-env.sh file.

JAVA_HOME is the location where java binaries are present.

American curl
  • 1,259
  • 2
  • 18
  • 21
1

I tried the above solutions but the following worked on me

export JAVA_HOME=/usr/java/default
Ajak6
  • 727
  • 5
  • 17
1

I tried changing /etc/environment:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

on the slave node, it works.

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
0

In some distributives(CentOS/OpenSuSe,...) will work only if you set JAVA_HOME in the /etc/environment.

nickc
  • 31
  • 2
0

Here we provide simple steps to setup JAVA_HOME while installing of Hadoop

Step1: Goto Java library path /lib jvm

Then set the JAVA_HOME & PATH in .bashrc file

Source : http://commandstech.com/hadoop-error-java_home-is-not-set-and-could-not-be-found-in-hadoop-installation/

Step 2 : Once it is done then go with Hadoop env file then update it.

After once it is done then stop the daemons and start daemons once again.

Spandana r
  • 213
  • 2
  • 3
-1

I solved this in my env, without modify hadoop-env.sh

You'd be better using /bin/bash as default shell not /bin/sh

Check these before:

  1. You have already config java and env (success echo $JAVA_HOME)
  2. right config hadoop

echo $SHELL in every node, check if print /bin/bash if not, vi /etc/passwd, add /bin/bash at tail of your username ref

Changing default shell in Linux

https://blog.csdn.net/whitehack/article/details/51705889

Greenonline
  • 1,330
  • 8
  • 23
  • 31
Santonio
  • 23
  • 4
-9
  • hadoop ERROR : JAVA_HOME is not set

Above error is because of the space in between two words.

Eg: Java located in C:\Program Files\Java --> Space in between Program and files would cause the above problem. If you remove the space, it would not show any error.