2010-08-05 3 views
0
import java.lang.Process;   
import java.io.*;  
import java.io.InuputStream;  
import java.io.IOException; 

public class newsmail{  
public static void main(String[] args) throws IOException{  
    String command = "java Newsworthy_RB";  
    Process child = Runtime.getRuntime.exec(command); 
      int c; 
    InputStream in = child.getInputStream(); 
    while((c=in.read())!=-1){ 
     System.out.print((char)c); 
    } 
    in.close(); 

    command = "java Newsworthy_CA"; 
    Process child = Runtime.getRuntime.exec(command); 
    InputStream in = child.getInputStream(); 
    while((c=in.read())!=-1){ 
     System.out.print((char)c); 
    } 
    in.close(); 
    } 

위의 코드에서 설명한대로 두 개의 Java 프로그램을 차례로 실행하고 있습니다. 첫 번째 프로그램 (Newsworthy_RB)에 오류가 발생하면 프로그램이 종료되어 오류가 표시됩니다. 대신 두 번째 프로그램 (Newsworthy_CA)을 계속 실행합니다. 친절하게 조언을 ... 오류 스트림을 얻기 위해 수행해야 무엇 실행 중에 오류가 인쇄되면 프로그램 중지

...

답변

1

당신은 명시 적으로 System.exit(status)를 호출을 programm을 중지 할 수 있습니다. 오류 상태 인 경우 표시하려면 != 0이어야합니다.

child.getErrorStream()을 통해 오류 스트림에 액세스 할 수 있습니다.

편집 : 실제 답변은 실제로하고 싶은 것에 따라 다릅니다. 그냥 확인하는 것입니다 목표 경우 첫 번째 programm에이 성공적으로 (종료) 종료 된 경우, 다음을 작성할 수 있습니다

현재 원인 :

public static void main(String[] args) throws Exception { 
    Process child1 = Runtime.getRuntime().exec("cmd"); 
    int status1 = child1.waitFor(); 

    System.out.println("Exit status of child one: " + status1); 

    // Something has gone wrong 
    if (status1 != 0) { 
     // end program 
     return; 
    } 

    Process child2 = Runtime.getRuntime().exec("cmd2"); 
    int status2 = child2.waitFor(); 

    System.out.println("Exit status of child two: " + status2); 
} 

Process#waitFor()이 (가) (의 javadoc에서 복사) 다음을 수행 필요에 따라서,이 Process 객체가 나타내는 프로세스가 종료 할 때까지 대기하는 thread 이 메소드는, 서브 프로세스가 벌써 종료하고있는 경우에 즉시 복귀합니다. 하위 프로세스가 아직 종료되지 않은 경우 하위 프로세스가 종료 될 때까지 호출 스레드가 차단됩니다.

+0

이 좀 더 설명하시기 바랍니다 ... 은 자바 초보자입니다 .... – LGAP

+0

누군가가 친절하게 .. – LGAP

+0

고마워 날 제발 도와 ... – LGAP

1

두 실행 사이의

if (child.waitFor() != 0) { 
    // print error message here 
    return; 
} 

을보십시오. waitFor은 연관된 프로세스가 리턴 될 때까지 현재 스레드를 대기시킵니다. here을 참조하십시오.

관련 문제