私は Marc B が提案した解決策を使用しました。この特定の状況の明確な例がオンラインにないため、将来誰かが必要になった場合に備えて、php と Java のコードを貼り付けます。
PHP コード:
<?php
$process_cmd = "java -jar test.jar";
$env = NULL;
$options = ['bypass_shell' => true];
$cwd = NULL;
$descriptorspec = [
0 => ["pipe", "r"], // stdin is a pipe that the child will read from
1 => ["pipe", "w"], // stdout is a pipe that the child will write to
2 => ["pipe", "w"] // stderr is a file to write to
];
$process = proc_open($process_cmd, $descriptorspec, $pipes, $cwd, $env, $options);
if (is_resource($process)) {
//feeding text to java
fwrite($pipes[0], "Test text");
fclose($pipes[0]);
//echoing returned text from java
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 "\n command returned $return_value\n";
}
?>
ジャワコード:
package Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input;
while ((input = br.readLine()) != null) {
System.out.println(input + " java test string ");
}
}
}