Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
/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!

share|improve this question
    
"logout and login back to see changes" if not try these Set JAVA_HOME as follows using syntax export JAVA_HOME=<path-to-java>. If your path is set to /usr/java/jdk1.5.0_07/bin/java, set it as follows: export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java Set PATH as follows: export PATH=$PATH:/usr/java/jdk1.5.0_07/bin Save and close the file. Just logout and login back to see new changes: $ echo $JAVA_HOME $ echo $PATH –  harish.venkat Dec 29 '11 at 17:33
1  
    
possible duplicate of pass 1 environment variable using sudo –  Gilles Dec 30 '11 at 21:50
add comment

2 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
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. –  Robert Hume 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. –  Gilles 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
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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