Synchronized (this) {
Process = Runtime.getruntime (). exec (CMDS);
}
Log process cache error messages
Final StringBuffer errorlog = new StringBuffer ();
Gets the error stream of the execution process
Final InputStream Errorstream = Process.geterrorstream ();
Final InputStream InputStream = Process.getinputstream ();
Threads that handle InputStream
New Thread () {
public void Run () {
BufferedReader in = new BufferedReader (new InputStreamReader (InputStream));
String line = null;
try {
while (line = In.readline ()) = null &&!errorlog.tostring (). Contains ("ERROR")) {
if (line! = null) {
Errorlog.append (line);
}
}
} catch (IOException e) {
throw new RuntimeException ("[Shell exec Error]:" + errorlog, E);
} finally {
try {
Inputstream.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}.start ();
Threads that handle Errorstream
New Thread () {
@Override
public void Run () {
BufferedReader err = new BufferedReader (new InputStreamReader (Errorstream));
String line = null;
try {
while (line = Err.readline ()) = null &&!errorlog.tostring (). Contains ("ERROR")) {
if (line! = null) {
Errorlog.append (line);
}
}
} catch (IOException e) {
throw new RuntimeException ("[Shell exec Error]:" + errorlog, E);
} finally {
try {
Errorstream.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}.start ();
Logger.info ("Wait for shell script execution to complete");
Thread.Sleep (1000);
abnormal termination
if (errorlog! = null && errorlog.length () > 0 && errorlog.tostring (). Contains ("ERROR")) {
Dispatchlogger.error ("[Shell exec Error]:" + errorlog);
throw new RuntimeException ("[Shell exec Error]:" + errorlog);
}
Process.waitfor (); Wait for shell script execution to complete
Runtime.getruntime (). EXEC () in Java, will cause blocking resolution