0

PHP in osx yosomite is unable to execute following code successfully.

<?php   
        exec("java -version 2>&1", $output, $returnValue);
        print_r($returnValue);
        print_r($output);
?>

The output I get is

1Array ( [0] => No Java runtime present, requesting install. [1] => 2015-07-16 21:25:05.588 java[1434:49304] JLRequestRuntimeInstall: Error calling: CFMessagePortCreateRemote )

I can run it successfully from command line. I tried changing the apache user but it didn't help.

Pushkar
  • 339
  • 3
  • 16

1 Answers1

2

In PHP, you don't have access to the normal search paths for executables such as Java; this is why you can run it in your terminal but not in the PHP code. It is a safety feature to not import the search paths in the shell $PATH variable to server-side languages such as PHP.

Although you could export your PATH variable into the exec subshell, you probably shouldn't. A full path for the java executable is preferred. Remember that the environment variables available to PHP (such as PATH) will be those for the user under which PHP runs -- usually a restricted user for security reasons.

If you're interested in going with the environment variable approach, I'd recommend a restricted environment. Depending upon your particular setup, the following question on SO (for Apache / PHP) may give you a starting point: https://stackoverflow.com/questions/13568191/how-to-get-system-environment-variables-into-php-while-running-cli-apache2hand

Community
  • 1
  • 1
rholmes
  • 4,064
  • 3
  • 25
  • 34
  • Thank you! Although I was using evnvironment variable approach, the problem was that I had java installed at multiple location and was pointing to wrong java. Once I made it to point to correct java, it started working. – Pushkar Jul 16 '15 at 17:30