Calling java from PHP exec

2019-01-09 05:08发布

问题:

I am doing the following in PHP:

exec('java -jar "/opt/flex3/lib/mxmlc.jar" +flexlib "/opt/flex3/frameworks" MyAS3App.as -default-size 360 280 -output MyAS3App.swf');

When I run this from the command line, it runs fine and finishes in a second or two.

When I run this command from PHP exec, the java process takes 100% CPU and never returns.

Any ideas?

I have also tried running the above command with '/usr/bin/java -Djava.awt.headless=true'.

I am running Mac OS X 10.5.5, MAMP 1.7, PHP 5.2.5

回答1:

Turns out it was a bug specific to the PHP stack MAMP (http://www.mamp.info/).

Turns out any invocation of the JVM following fails under MAMP, e.g.:

exec('java -version');

The fix is to prefix the command with

export DYLD_LIBRARY_PATH="";

Also I realized there's no reason to use that method of invoking mxmlc.

So here's the final, working command:

exec('export DYLD_LIBRARY_PATH=""; mxmlc MyAS3App.as -default-size 360 280 -output MyAS3App.swf');


回答2:

I manage to get this to work togheter with MAMP. The solution was to include the:

export DYLD_LIBRARY_PATH="";
in the exec call:

$argss = "export DYLD_LIBRARY_PATH=\"\"; /usr/bin/java -jar /Applications/yourjarfile.jar";
$resultXML = exec($argss, $output);


回答3:

Is there a reason why your using the mxmlc jar file to compile your flex application? have you tried using the executable or an ant task, instead?

Maybe the compiling is taking too long so that your PHP script times out?



回答4:

Exec is always tricky, on any language :-)

Try to:

  • use background execution (add & symbol at the end)
  • use shell_exec instead
  • specify the full path to java executable (may be the one available to PHP is not the one you need?)
  • run a simple HelloWorld java app to see if the problem is in Java or in mxmlc specifically

It's strange that java takes 100% CPU. I cannot explain it with any common mistake made when using exec()... try to send it a SIGQUIT to dump the threads, then read the dump -- may be you'll figure something out.



标签: java php flex exec