gpt4 book ai didi

php - 如何运行 Java 程序并在 PHP 中获取输出?

转载 作者:可可西里 更新时间:2023-11-01 12:18:09 26 4
gpt4 key购买 nike

我想运行类似的东西(在 myProgram.sh 中):

java -cp whatever.jar com.my.program $1

在 PHP 中读取输出。

到目前为止,我有类似的东西:

$processOrderCommand = 'bash -c "exec nohup setsid /myProgram.sh ' . $arg1 . ' > /dev/null 2>&1 &"';
exec($processOrderCommand);

但我真正想要的是能够在 PHP 脚本中获取 java 程序的输出,而不仅仅是将其作为另一个线程执行。

如何做到这一点?

最佳答案

你可以这样做:

exec($processOrderCommand, $output);

来自文档:

If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

为了更好地控制您的执行,您可以查看 proc_open()


资源:

关于php - 如何运行 Java 程序并在 PHP 中获取输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3789691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com