2010-04-24 2 views
3

왜 이것이 걸려 있는지 알 수 없습니다. commons-exec를 통해 실행되는 프로세스에서 출력을 캡처하려고 시도하고 있으며 계속 중단합니다. 아래에서이 동작을 보여주기위한 예제 프로그램을 제공했습니다. 반면에 비동기 인commons-exec : executor.execute (commandLine)를 호출하면 응답하지 않습니다.

import java.io.DataInputStream; 
import java.io.IOException; 
import java.io.PipedInputStream; 
import java.io.PipedOutputStream; 
import org.apache.commons.exec.CommandLine; 
import org.apache.commons.exec.DefaultExecutor; 
import org.apache.commons.exec.ExecuteException; 
import org.apache.commons.exec.PumpStreamHandler; 
public class test { 

public static void main(String[] args) { 
    String command = "java"; 

    PipedOutputStream output = new PipedOutputStream(); 
    PumpStreamHandler psh = new PumpStreamHandler(output); 

    CommandLine cl = CommandLine.parse(command); 

    DefaultExecutor exec = new DefaultExecutor(); 
    DataInputStream is = null; 
    try { 
     is = new DataInputStream(new PipedInputStream(output)); 
     exec.setStreamHandler(psh); 
     exec.execute(cl); 
    } catch (ExecuteException ex) { 
    } catch (IOException ex) { 
    } 

    System.out.println("huh?"); 
} 
} 

답변

9

javadoc에 따르면 execute(CommandLine command)execute(CommandLine command, ExecuteResultHandler handler) 동기이다.

5

사용자가 호출 한 명령 java은 출력을 표준 출력 스트림으로 생성합니다. 이 스트림은 호출하는 프로그램에 의해 입력 스트림으로 펌핑되어야합니다. 이것은 프로그램에서 발생하지 않습니다.

코드 된 입력 스트림 (코드에서 is)은 파이프 된 스트림이 작동하는 방법이므로 별도의 스레드에서 읽어야합니다. execute()을 호출하기 전에 읽기 스레드를 시작해야합니다. 당신이 파이프 스트림을 사용해야하며 출력으로 ByteArrayInputStream를 사용하는 간단한 방법을 사용할 수 없도록

, 당신은 대용량 데이터를 기대 Streaming output with commons-exec? 다른 질문에 따라도 Capturing large amounts of output from Apache Commons-Exec

참조하십시오. 거기서 자신에게 준 대답은 여기에있는 코드와 동일한 문제를 겪고 있습니다.