0
に渡すので、実行中に入力したコマンドで動作するJavaコンソールがあります。
これはPHPでも可能ですか? jarを実行することはexec()と一緒ですが、実行中のjarコマンドを渡すことはできません。実行中のjarコマンドをphp
に渡すので、実行中に入力したコマンドで動作するJavaコンソールがあります。
これはPHPでも可能ですか? jarを実行することはexec()と一緒ですが、実行中のjarコマンドを渡すことはできません。実行中のjarコマンドをphp
あなたがしたいことは、exec()の代わりにproc_open()でjarを初期化することです。 proc_open()を使うと、Javaプロセスのstdin/stdout/stderrに対して読み書きを行う個々のストリームを持つことができます。したがって、Javaプロセスを開始し、fwrite()を使用してJavaプロセスのstdin($pipes[0]
)にコマンドを送信します。詳細は、proc_open()のドキュメントページの例を参照してください。
$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";
}
:
EDITは、ここでは簡単のサンプルコード(は、proc_openドキュメント上の例のちょうど軽く修正版)です