Java 用php传递正在运行的jar命令
因此,我有一个Java控制台jar,它与我在运行时输入的命令一起工作。Java 用php传递正在运行的jar命令,java,php,Java,Php,因此,我有一个Java控制台jar,它与我在运行时输入的命令一起工作。 PHP也可以这样做吗?我知道用exec()执行jar是可行的,但我无法真正传递正在运行的jar命令或获取其输出。您要做的是用exec()而不是exec()初始化jar。proc_open()允许您从Java进程的stdin/stdout/stderr读取/写入单个流。因此,您将启动Java进程,然后使用fwrite()向Java进程的stdin($pipes[0])发送命令。有关更多信息,请参阅proc_open()的文档页
PHP也可以这样做吗?我知道用exec()执行jar是可行的,但我无法真正传递正在运行的jar命令或获取其输出。您要做的是用exec()而不是exec()初始化jar。proc_open()允许您从Java进程的stdin/stdout/stderr读取/写入单个流。因此,您将启动Java进程,然后使用fwrite()向Java进程的stdin(
$pipes[0]
)发送命令。有关更多信息,请参阅proc_open()的文档页面上的示例
编辑这里的一个快速代码示例(只是proc_open docs上示例的一个稍加修改的版本):
您要做的是用而不是exec()初始化jar。proc_open()允许您从Java进程的stdin/stdout/stderr读取/写入单个流。因此,您将启动Java进程,然后使用fwrite()向Java进程的stdin(
$pipes[0]
)发送命令。有关更多信息,请参阅proc_open()的文档页面上的示例
编辑这里的一个快速代码示例(只是proc_open docs上示例的一个稍加修改的版本):
$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
);
$process = proc_open('java -jar example.jar', $descriptorspec, $pipes);
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], 'this is a command!');
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";
}