2011-04-21 3 views
0

자바 코드 내에서 jar 파일 (java -classpath path/file.jar classname 사용)의 어딘가에있는 클래스를 호출합니다.inputStream을 읽을 때 getRuntime.exec()가 차단되지 않도록하려면 어떻게해야합니까?

내 문제는 genKOSCommand 명령이 유효하지 않은 경우 input.readLine()을 호출하면 프로그램이 차단됩니다. 따라서 차단을 피하기 위해 input.ready()을 추가했습니다. 프로그램을 디버깅 할 때 괜찮습니다. 일하는 것 같습니다. 그러나 디버그에서 실행하지 않으면 버퍼가 준비되지 않습니다.

 // Execute a command with an argument that contains a space 
     String[] genKOSCommand = new String[] { 
       "java", 
       "-classpath", 
       Config.XDSI_TEST_KIT_HOME + "/xdsitest/lib/xdsitest.jar;" 
         + Config.XDSI_TEST_KIT_HOME + "/xdsitest/classes", 
       "ca.etsmtl.ihe.xdsitest.docsource.SimplePublisher", "-k", 
       "C:/Softmedical/Viewer_Test/xdsi-testkit-2.0.4/xdsihome/usr/data/image14.dcm" }; 

     Process child = Runtime.getRuntime().exec(genKOSCommand); 

     BufferedReader input = new BufferedReader(new InputStreamReader(
       child.getInputStream()), 13107200); 

     String line = null; 

     if (input.ready()) { 
      while ((line = input.readLine()) != null) { 
       System.out.println(line); 
      } 

      try { 
       child.waitFor(); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

실행 된 명령의 문제를 감지하는 방법에 대한 조언이 있으십니까?

감사합니다.

+1

이전 질문에서 제안한 것처럼 다른 스트림을 사용하여 각 스트림에서 읽을 수 있습니다. – MByD

+0

@MByD 그래서 스트림을 읽으려면 스레드를 시작하면 내 주요 프로세스를 차단하지 않습니다. 하지만 readLine은 여전히 ​​스레드를 차단합니다. 그렇습니까? (반복) –

+0

이 글은 여러 번 응답되었으며, 정식 기사는 [here] (http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html)입니다. – jtahlborn

답변

-2

BufferedReader를 준비하려면 루프를 기다려야합니다.

while (input.ready() == false) { /* intentional empty space here */ } 

while ((line = input.readLine()) != null) { 
    System.out.println(line); 
} 
/* rest of code follows */ 
관련 문제