Runtime.getruntime (). EXEC () in Java, will cause blocking resolution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.