While setting up Nutch on a Mac, I kept getting this error in the terminal:

Error: JAVA_HOME is not set

To fix the error on OS X 10.6.x (Snow Leopard), enter the following at the command prompt:

export JAVA_HOME=$(/usr/libexec/java_home)

On 10.5 and above, the command line tool /usr/libexec/java_home dynamically finds the top Java version specified in Java Preferences for the current user. Using this method will allow the path to automatically change as software updates change Java versions.

So, if you try to run a command or script, and get the error (shown in red). . .

yourmac:~ username$ /Users/username/Sites/apache-nutch-1.1/bin/nutch inject crawl/crawldb /Users/username/Sites/apache-nutch-1.1/urls
Error: JAVA_HOME is not set.

. . .set the Java_Home environment variable, and then try to run the command again. This time the response is the correct output for the command (shown in green.)

yourmac:~ username$ export JAVA_HOME=$(/usr/libexec/java_home)
yourmac:~ username$ /Users/username/Sites/apache-nutch-1.1/bin/nutch inject crawl/crawldb /Users/username/Sites/apache-nutch-1.1/urls
Injector: starting
Injector: crawlDb: crawl/crawldb
Injector: urlDir: /Users/username/Sites/apache-nutch-1.1/urls
Injector: Converting injected urls to crawl db entries.
Injector: Merging injected urls into crawl db.
Injector: done
yourmac:~ username$ 

It only lasts for one session. To set it permanently, you can add:

JAVA_HOME=/usr/libexec/java_home
export JAVA_HOME;

to the file ~/.profile

Screen shot 2010-08-26 at 5.51.38 PM.jpg

To confirm you have java, enter this into the Terminal:

java -version

To determine the real path:

echo $JAVA_HOME