Java – Call java from PHP exec

Call java from PHP exec… here is a solution to the problem.

Call java from PHP exec

I’m 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 it from the command line, it works fine and completes in a second or two.

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

Any ideas?

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

I’m running Mac OS X 10.5.5, MAMP 1.7, PHP 5.2.5

Solution

It turns out that this is a bug specific to the PHP stack MAMP ( http://www.mamp.info/ ).

It turns out that any call to the JVM under MAMP fails, for example:

exec('java -version');

The fix is to precede the command

export DYLD_LIBRARY_PATH="";

I also realized that there is no reason to use a method that calls mxmlc.

So this is the last working order :

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

Related Problems and Solutions