December 8, 2009

sudo visudo and environment variables like JAVA_HOME

Filed under: bash, java, osx, tip — michaelangela @ 3:26 am

I was trying all sorts of incantations to get JAVA_HOME set. None worked. OS X. Bash. .bashrc, .profile, etc., etc. Turns out the following is the problem, and the solution.

sudo, JAVA_HOME and Mac OS X [Article] « elc technologies

The problem is that JAVA_HOME doesn’t get passed to sudo, so sudo cannot access it. I remembered two solutions to fix this problem, one by telling the env to keep the variable and the other by switching to root user and exporting the variable there.

So, the first solution is to make the JAVA_HOME variable available to sudo, first export it and then run sudo visudo and add the following line to it:

Defaults    env_keep +="JAVA_HOME"

In the end, it had to go in /etc/profile for it to take as noted below. Maven (mvn -v) just would not show the new version of java otherwise and builds done with maven would fail because they targeted the wrong version as well.

Compiling Java 1.6 projects using Maven on Mac OS X

You can declare this either as a one time export in your current shell above, in /etc/profile as I normally do or in any other startup file of your choice. Afterwards be sure to refresh your shell before you try again. This can be done either by closing your current terminal window and opening a new one or by sourcing whichever file you’ve put the above information into.

source /etc/profile

In order to make sure that it has taken effect one can output it in the shell as follows.


If it shows nothing then the shell has not picked up your change.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: