Runtime.getruntime(cmd.exe C () solution to output stream Blocking

Source: Internet
Author: User

Java uses runtime.getruntime(cmd.exe C () to call an external program and obtain the "standard output stream", which is always blocked. I found it online and thought it was a "wrong output stream" problem. sure enough, the "error output stream" is read by a single thread, and the "standard output stream" is no longer blocked. the source code is as follows:

 /** Execute an external program and obtain the standard output */<br/> public static String excute1__multithread (String [] cmd, String encoding) <br/>{< br/> BufferedReader bReader = null; <br/> InputStreamReader sReader = null; <br/> try <br/>{< br/> Process p = runtime.getruntime(cmd.exe c (cmd ); </p> <p>/* indicates that the error output stream is read by a separate thread, otherwise, the standard output stream may be blocked */<br/> Thread t = new Thread (new InputStreamRunnable (p. getErrorStream (), "ErrorStream"); <br/> t. start (); </p> <p> /* "Standard output stream" is read in the current method */<br/> BufferedInputStream bis = new BufferedInputStream (p. getInputStream (); </p> <p> if (encoding! = Null & encoding. length ()! = 0) <br/>{< br/> sReader = new InputStreamReader (bis, encoding ); // set the encoding method <br/>}< br/> else <br/> {<br/> sReader = new InputStreamReader (bis, "GBK "); <br/>}< br/> bReader = new BufferedReader (sReader); </p> <p> StringBuilder sb = new StringBuilder (); <br/> String line; </p> <p> while (line = bReader. readLine ())! = Null) <br/>{< br/> sb. append (line); <br/> sb. append ("/n"); <br/>}</p> <p> bReader. close (); <br/> p. destroy (); <br/> return sb. toString (); <br/>}< br/> catch (Exception e) <br/>{< br/> e. printStackTrace (); <br/> return ErrorString; <br/>}< br/> finally <br/>{< br/>}</p> <p>/** thread for reading InputStream * /<br/> class InputStreamRunnable implements Runnable <br/>{< br/> BufferedReader bReader = null; <Br/> String type = null; <br/> public InputStreamRunnable (InputStream is, String _ type) <br/>{< br/> try <br/> {<br/> bReader = new BufferedReader (new InputStreamReader (new BufferedInputStream (is), "UTF-8 ")); <br/> type = _ type; <br/>}< br/> catch (Exception ex) <br/>{< br/>}< br/> public void run () <br/>{< br/> String line; <br/> int lineNum = 0; </p> <p> try <br/> {<br/> while (line = bReader. ReadLine ())! = Null) <br/>{< br/> lineNum ++; <br/> // Thread. sleep (200); <br/>}< br/> bReader. close (); <br/>}< br/> catch (Exception ex) <br/>{< br/>}< br/>}

 

In addition, runtime.getruntime(cmd.exe C () has some limitations, that is, it cannot execute complicated commands like cmd. For example, the redirection of the output stream, such:

Runtime.getruntime.exe C ("xx.exe yy.doc> zz.txt ");

 

He will return immediately and will not execute it. But we can do this to do the same job in cmd:

Runtime.getruntime.exe C ("CMD/C xx.exe yy.doc> zz.txt ");

 

Specifically,/C is "the command for executing the string following ". this is OK, but you still need to pay attention to the problem of "error output stream", and still need to open a single thread to read. otherwise, it will be blocked.

 

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.