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();
}
I am executing two java programs one after the other as given in the above code.
If any error occurs in the first program(Newsworthy_RB), my program should terminate displaying the error. Instead it continues to the execution of second program(Newsworthy_CA).
What should be done to get the error stream… Kindly advise…
You can stop the programm by explicitly calling
System.exit(status). In case of an error status should be!= 0to indicate it.You can access the error stream via
child.getErrorStream().EDIT: The actual answer depends on what you really want to do. If you aim is just to check, if the first programm sucessfully terminated (ended), you can write the following:
Process#waitFor()does the following (copied from javadoc):