2016-06-16 2 views
0

그래서 실행 중에 입력하는 명령으로 작동하는 Java 콘솔 항아리가 있습니다.
PHP로도 가능합니까? 항아리를 실행하는 것은 exec()를 사용하는 것이지만 실제로 실행중인 jar 명령을 전달하거나 출력을 얻을 수는 없습니다.PHP로 실행중인 jar 명령을 전달하십시오.

답변

2

jar를 exec() 대신 proc_open()으로 초기화하는 것이 좋습니다. 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"; 
} 
:

편집 여기에 빠른 코드 샘플합니다 (proc_open 문서의 예를 단지 약간 수정 된 버전)입니다

관련 문제