2017-02-11 1 views

답변

1

이 this-

String command = "test.sh"; 
Process process; 
process = Runtime.getRuntime().exec(command); 
process.waitFor(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
String response= ""; 
while ((response= reader.readLine()) != null) { 
    System.out.println(response); 
} 
+0

나는 이것을 시험해 보았지만 기다리기 시작하고 결코 돌아 오지 않는다. \t \t process = Runtime.getRuntime(). exec (command); \t \t System.out.println ("first line"); \t \t process.waitFor(); \t \t System.out.println ("두 번째 줄"); 이 코드에서 두 번째 줄은 결코 –

+0

을 인쇄하지 않습니다. 명령 프롬프트를 통해 실행하고 있습니까? – deen

0

당신은 ProcessBuilder 클래스에 모습을 가질 수하려고 사전에 test.sh

read -p "What is your name?" name 
echo $name 
read -p "What is your age?" age 
echo $age 
echo 'done' 

덕분에 내 간단한 스크립트입니다

ProcessBuilder pb = new ProcessBuilder("/path_to_your_script.sh"); 
    pb.inheritIO(); // redirect input and output 
    Process p = pb.start(); 
    p.waitFor(); // allow to wait the end of your process 

그러나 스크립트 예제에서 동시에 입력 및 출력 명령 인 read -p 명령이 있습니다. 그래서 문제가 생길 수 있습니다.

+0

Fabien에게 감사드립니다, 내 대답을 얻었습니다. 명령이 내게 해줬다고 생각합니다 :) –

관련 문제