通过 cmd 提示符从 PHP 执行 .jar 文件并捕获输出

发布于 2024-09-10 06:59:24 字数 444 浏览 3 评论 0原文

我的应用程序有一个 jar 文件,其中包含多个类。 PHP 通过命令提示符调用该 jar 文件。我使用以下 PHP 代码片段来调用 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 工作正常,当直接从命令提示符调用时,它工作正常。

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

I have a jar file of my application which has more than one class. The jar file is called by PHP through the command prompt. I am using the following PHP snippet to call the jar file.

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

The problem here is interesting. I am able to get the 'System.out.println' statements which are in the main function. But unable to get the output statements from other classes.

I have tried with exec() also. The .jar is working fine and when called from the command prompt directly, its working fine.

Is there a way to capture the whole output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角迷惘 2024-09-17 06:59:24

您是否尝试过使用 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";
}
?>

Have you tried using proc_open instead:

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

it allows you to setup the pipes "Will be set to an indexed array of file pointers that correspond to PHP's end of any pipes that are created."

Example from 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";
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文