从 PHP exec 调用 java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/278868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:45:14  来源:igfitidea点击:

Calling java from PHP exec

javaphpapache-flexexec

提问by Keeth

I am doing the following in PHP:

我在 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.

当我从 PHP exec 运行此命令时,java 进程占用 100% CPU 并且永远不会返回。

Any ideas?

有任何想法吗?

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

我还尝试使用“/usr/bin/java -Djava.awt.headless=true”运行上述命令。

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

我正在运行 Mac OS X 10.5.5、MAMP 1.7、PHP 5.2.5

回答by Keeth

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

原来这是一个特定于 PHP 堆栈 MAMP ( http://www.mamp.info/) 的错误。

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

事实证明,在 MAMP 下对 JVM 的任何调用都失败了,例如:

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.

我也意识到没有理由使用调用 mxmlc 的方法。

So here's the final, working command:

所以这是最终的工作命令:

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

回答by Pontus

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

我设法让它与 MAMP 一起工作。解决方案是包括:

export DYLD_LIBRARY_PATH="";
in the exec call:

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

回答by mmattax

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?

您是否有理由使用 mxmlc jar 文件来编译您的 flex 应用程序?您是否尝试过使用可执行文件或 ant 任务?

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

也许编译时间太长,以至于您的 PHP 脚本超时?

回答by Vladimir Dyuzhev

Exec is always tricky, on any language :-)

Exec 在任何语言上总是很棘手:-)

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
  • 使用后台执行(在最后添加 & 符号)
  • 改用 shell_exec
  • 指定 java 可执行文件的完整路径(可能是 PHP 可用的路径不是您需要的路径?)
  • 运行一个简单的 HelloWorld java 应用程序来查看问题是在 Java 中还是在 mxmlc 中

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占用了100%的CPU。我无法用使用 exec() 时犯的任何常见错误来解释它……尝试向它发送一个 SIGQUIT 以转储线程,然后读取转储——也许你会想出一些办法。