【问题标题】:Executing .jar file from PHP through cmd prompt and capturing output通过 cmd 提示符从 PHP 执行 .jar 文件并捕获输出
【发布时间】:2010-07-16 04:44:48
【问题描述】:

我的应用程序有一个 jar 文件,其中包含多个类。 jar 文件由 PHP 通过命令提示符调用。我正在使用下面的 PHP sn-p 来调用 jar 文件。

<?php
$result=popen('java -jar D:\\Development\\Filehandler\\dist\\Filehandler.jar getConfigLang', "r");
while(!feof($result)){
  print fread($result, 1024);
  flush();
}
fclose($result);
?>

这里的问题很有趣。我能够获得主函数中的“System.out.println”语句。但无法获取其他类的输出语句。

我也尝试过 exec()。 .jar 工作正常,当直接从命令提示符调用时,它工作正常。

有没有办法捕获整个输出?

【问题讨论】:

  • 您是否尝试过先不使用 PHP 运行?
  • 是的,直接在命令提示符下尝试效果很好

标签: java php popen


【解决方案1】:

您是否尝试过使用 proc_open 代替:

http://au.php.net/manual/en/function.proc-open.php

它允许您设置管道“将被设置为文件指针的索引数组,这些文件指针对应于 PHP 所创建的任何管道的结尾。”

来自 php.net 的示例

<?php
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);

$cwd = '/tmp';
$env = array('some_option' => 'aeiou');

$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to /tmp/error-output.txt

    fwrite($pipes[0], '<?php print_r($_ENV); ?>');
    fclose($pipes[0]);

    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);

    echo "command returned $return_value\n";
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多