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

发布于 2024-09-24 16:25:58 字数 392 浏览 1 评论 0原文

我想

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 技术交流群。

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

发布评论

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

评论(2

心房的律动 2024-10-01 16:25:58

您可以这样做:

exec($processOrderCommand, $output);

从文档中:

如果存在output参数,则指定的数组将填充命令输出的每一行。此数组中不包含尾随空格,例如 \n。请注意,如果数组已包含一些元素,exec() 将追加到数组末尾。如果您不希望函数追加元素,请调用 unset()在将数组传递给 exec() 之前先对其进行处理。

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


资源:

You can do this :

exec($processOrderCommand, $output);

From the documentation :

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().

For a better control on your execution you can take a look at proc_open()


Resources :

烧了回忆取暖 2024-10-01 16:25:58

关键是类路径在 shell_exec 中必须是绝对路径
PHP 脚本。

或者至少这是我让它正常工作的唯一方法。基本上,几乎不可能在不同的环境中区分 php 脚本运行 JVM 的相对目录是什么。

此外,它还有助于放置 java 的绝对路径位置,例如 usr/.../bin/java

The key is that the classpaths need to be absolute within the shell_exec
PHP script.

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

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