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.

3
  • Java is present on the system. Commented Jul 16, 2015 at 15:59
  • Providing absolute path to java executable might help. Commented Jul 16, 2015 at 16:14
  • It helped but can't it be done without the full path? Commented Jul 16, 2015 at 16:31

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

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.