MacOS – why isn’t the environment variable visible from the command line

javamacosunix

I'm trying to run the james smtp server on Lion. When I do this …

sudo JAVA_HOME=/Library/Java/Home ./james-2.3.2/bin/run.sh

… it works fine.

However, I'd like to make JAVA_HOME an environment variable so it is visible to other applications too.

I edited ~/.bash_profile to look like this …

export JAVA_HOME=/Library/Java/home

… and the variable appears to be visible from the command line like so …

$ echo $JAVA_HOME
/Library/Java/home

But when I run james again like this, I get a warning about JAVA_HOME …

$ sudo ./james-2.3.2/bin/run.sh
ERROR: JAVA_HOME not found in your environment.

Please, set the JAVA_HOME variable in your environment to match the
location of the Java Virtual Machine you want to use.

Any ideas what I'm doing wrong? Why can't the command to run james see the JAVA_HOME environment variable?

Any info is greatly appreciated, thanks!

Rob

Best Answer

The issue is that you are not running in your environment so any changes to ~/.bash_profile will have no effect on the command. It will be visible to other processes

sudo runs as another user - in this case root as you have not provided a -u username parameter. Thus you need to change the root environment to make JAVA_HOME visible to the run.sh command