1

If I run

which java

from the command line I get the proper input (/usr/java/.../bin/java). However if I run it in a php script:

<?
  echo 'java. ' . shell_exec('which java');
  echo 'ls. ' . shell_exec('which ls');
?>

nothing gets printed out for which java but I get the proper results for which ls...

2
  • 9
    The server process doesn't have it's path environment variable set. Commented Sep 24, 2010 at 19:33
  • If I send the hardcoded path it works. I then tried to include it in a bash file script with the environment setup and it didn't work either... Commented Sep 25, 2010 at 16:05

2 Answers 2

2

Two things were needed:

  • the full path to the JVM (it wasn't set in the environment)
  • " 2>&1" at the end of the command line.

So for example:

echo shell_exec('/usr/java/jdk6/bin/java -cp myJars.jar MyMainClass arg1 2>&1");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, just what I was looking for during the past 30 minutes. Adding the 2>&1 worked. Not sure why though...
1

I just ran into this problem as well. I was trying to determine if the qrencode utility is installed on the (any) server, and if not then log/warn/exit gracefully.

Considering the program should always be in a standard path, I prefixed the which command with the likely locations of the binary, while still respecting that $PATH might be somehow defined on the system. I think you could use this approach with common Java paths, too:

shell_exec('PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin" '.
           'which qrencode');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.