如何在 PHP 中运行 Java 程序并获取输出?
我想
java -cp whatever.jar com.my.program $1
在 PHP 中运行类似 (in myProgram.sh): 的内容并读取输出。
到目前为止,我有这样的想法:
$processOrderCommand = 'bash -c "exec nohup setsid /myProgram.sh ' . $arg1 . ' > /dev/null 2>&1 &"';
exec($processOrderCommand);
但我真正想要的是能够在 PHP 脚本中获取 java 程序的输出,而不仅仅是将其作为另一个线程执行。
这怎么能做到呢?
I'd like to run something like (in myProgram.sh):
java -cp whatever.jar com.my.program $1
within PHP and read the output.
So far I have something like:
$processOrderCommand = 'bash -c "exec nohup setsid /myProgram.sh ' . $arg1 . ' > /dev/null 2>&1 &"';
exec($processOrderCommand);
But what I'd really like is to be able to get the output of the java program within the PHP script and not just execute it as another thread.
How can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
从文档中:
为了更好地控制执行,您可以查看
proc_open ()
资源:
exec()
proc_open()
You can do this :
From the documentation :
For a better control on your execution you can take a look at
proc_open()
Resources :
exec()
proc_open()
或者至少这是我让它正常工作的唯一方法。基本上,几乎不可能在不同的环境中区分 php 脚本运行 JVM 的相对目录是什么。
此外,它还有助于放置 java 的绝对路径位置,例如 usr/.../bin/java
Or at least that's the only way I could get it to correctly work. Basically it's almost impossible to tell from environment to environment what the relative directory is that the php script is running the JVM.
As well, it helped to put the absolute path location for java, such as usr/.../bin/java