2011-09-01 4 views
0

명령 행에서 명령을 실행해야하며 명령을 실행 한 후에 예 또는 아니오 응답을 제공해야합니다. 예를 들어 :exec를 사용하여 명령에 다중 입력 보내기


> ./ somecommand (새 창)

당신이 이것에 대해 확실 해요? [예/아니오] : 예 (반품)

정말 이에 대해 확실합니까? [예/아니오] : 예 (반품)

마지막 기회. [예/아니오] : 아니요 (반품)

확인.


이 작업을 수행하는 데 다음 코드 단편을 사용하려고합니다.

try { 
    // Execute command 
    String command = "somecommand"; 
    Process child = Runtime.getRuntime().exec(command); 

    // Get output stream to write from it 
    OutputStream out = child.getOutputStream(); 

    out.write("yes".getBytes()); 

    out.flush();  
    out = child.getOutputStream(); 
    out.write("yes".getBytes()); 

    out.flush();  
    out = child.getOutputStream(); 
    out.write("no".getBytes()); 

    out.close(); 
} catch (IOException e) { 
} 

당신이 볼 수 있듯이, 나는 각 쓰기 후 '밖으로'홍조, '= child.getOutputStream() 밖으로'세 번 사용했습니다. 하지만 그건 효과가없는 것 같습니다. 어떻게 할 수 있었는지 생각해보십시오.

+0

다음 게시물을 확인하십시오 : http://stackoverflow.com/questions/2877609/in-java-send-commands-to-another-command-line-program –

+0

'somecommand'는'--assume-yes '또는 이와 유사한 옵션? – trashgod

+0

아니요 .. 프로세스 출력 결과를 기반으로 질문에 대한 답변을 제공해야합니다. –

답변

1

while 루프에서 java readLine() 명령을 사용해보십시오. Yes 또는 No과 같은 종료 조건을 지정하십시오.