I'm trying to run the James SMTP server on Lion (Mac OS X 10.7.2). 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

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!


sudo resets the environment variables to safe defaults.

If you want to preserve that variable, you need to visudo and add this:

Defaults env_keep = "JAVA_HOME"

Please note that carrying user environment variables over to privileged accounts is a possible security risk.

  • 1
    I'd say that this is the correct reason. But to carry over an environment variable from the user environment is a security hole. I'd make sure to say that in your answer if you suggest editing the sudoers file.
    – Arcege
    Dec 30 '11 at 12:44
  • Of course you are perfectly right. I edited the answer.
    – Patrick
    Dec 30 '11 at 12:48
  • Maybe the question I should ask, then, is how should I run the james smtp as me instead of with sudo? I'm running with sudo because james uses port 25 which I understand is not available to application that are not run by root. Is that correct? Any info is appreciated. Dec 30 '11 at 17:13
  • @Arcege Carrying environment variables over is only a security risk when you allow a user specific commands through sudo. If a user can run arbitrary commands through sudo, the erasure of environment variables is mostly a pointless annoyance. Dec 30 '11 at 21:49
  • @Gilles, with a wide open sudoers, that's true. But it probably not a good practice to say "lets open up to allow a redirector EV like JAVA_HOME" as a general statement without mentioning that there are some possible security risks.
    – Arcege
    Dec 31 '11 at 0:41

I am not sure whether Lion has an /etc directory or not (I assume it does) or whether /etc/bashprofile or /etc/bashrc is actually in that directory, but if you want to make this change system-wide, just look for either of these files, edit it, and make the change there as so:

export PATH=$PATH:/path/to/Java/

Next log in will pick up the change and will be persisted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.