I have installed ServiceMix on my machine, and am trying to start it. The startup script is called start.sh. When I did ./start.sh, it gave me a Permission Denied message. So I used sudo ./start.sh, which starts the server fine.

However, it seems to be using Java 7, when I would like to use Java 6 (for various reasons). I looked through the start.sh script and found that it uses the JAVA_HOME variable. So I set my JAVA_HOME to the JDK 6 directory.

However, ServiceMix still uses Java 7.

A Google search revealed that when a command is run with sudo, sudo removes certain environment variables. So I did sudo -E ./start.sh. Again, it used JDK 7.

I opened the start.sh and added a line, echo $JAVA_HOME to see whether the variable is set properly or not. The output of echo was an empty line.

I tried modifying the /etc/sudoers file to include Defaults env_keep += "JAVA_HOME". Executing start.sh now tells me that my echo statement is again an empty line. ServiceMix still uses JDK 7.

I tried setting JAVA_HOME in /etc/profile. Executing start.sh now tells me that my echo statement is again an empty line. Again, ServiceMix uses JDK 7.

What mistake am I doing here?

FWIW, am on Mac OS X 10.8.2. echo $SHELL on my terminal returns /bin/bash. When I use sudo su and fire echo $SHELL, it returns /bin/sh.

  • 1
    Why not just set JAVA_HOME explicitly in start.sh?
    – Tim B
    Mar 10, 2013 at 15:31
  • Why not sudo chmod the start.sh file so that the program can run at user-level privileges Mar 12, 2013 at 6:14
  • Did you export JAVA_HOME? If it's not an environment variable, sudo -E won't keep it. May 13 at 4:32
  • I don't know for sure, but also try export JRE_HOME=$JAVA_HOME May 13 at 4:34

2 Answers 2


I would suggest you to try out the following steps:

  1. Inside /etc/profile.d directory, create a file named set_java_path.sh file containing the line

    export JAVA_HOME=<_complete_path_to_java6_directory_>

  2. Run bash command once to make the above change to load. And run the start.sh script.


If the /etc/profile.d directory doesn't exist, create a directory names same (permission: 0755 & owner and group as root) and inside your /etc/profile file, add the following code:

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
  unset i
  • I don't seem to have a /etc/profile.d directory. Should I create it? Mar 10, 2013 at 6:48
  • @ShivasubramanianA: I have updated the answer. Mar 10, 2013 at 6:59

To preserve your environment variables as sudoer add Defaults env_keep += PATH to your sudoer file (sudo visudo).

  • i upvoted for the intent of the answer, and because I learned something new. note that OP is asking about JAVA_HOME, not PATH, though that's trivial and the answer would be the same. May 14, 2013 at 16:58

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.