Blocking problem encountered when java executes the bat command

Source: Internet
Author: User

Use Java to execute the bat command. If the bat operation takes too long, blocking may occur and bat will not be executed until the server is shut down.

For example:

Runtime r=Runtime.getRuntime();Process p=null;try{String path = "D:/test.bat";
P = r.exe c ("cmd.exe/c" + path); p. waitFor ();} catch (Exception e) {System. out. println ("running error:" + e. getMessage (); e. printStackTrace ();}


Generally, java exec does not help you deal with thread blocking and needs to be handled manually.

After processing:

Runtime r=Runtime.getRuntime();Process p=null;try{String path = "D:/test.bat";
p = r.exec("cmd.exe /c  "+path);
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");                  errorGobbler.start();         StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");         outGobbler.start();
P. waitFor ();} catch (Exception e) {System. out. println ("RUN error:" + e. getMessage (); e. printStackTrace ();}

StreamGobblerClass:

Package com. test. tool; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java. io. outputStream; import java. io. printWriter;/*** used to process error streams generated by runtime.getruntime(cmd.exe c and output streams */public class StreamGobbler extends Thread {InputStream is; String type; OutputStream OS; StreamGobbler (InputStream is, String type) {this (is, type, null);} Stream Gobbler (InputStream is, String type, OutputStream redirect) {this. is = is; this. type = type; this. OS = redirect;} public void run () {InputStreamReader isr = null; BufferedReader br = null; PrintWriter pw = null; try {if (OS! = Null) pw = new PrintWriter (OS); isr = new InputStreamReader (is); br = new BufferedReader (isr); String line = null; while (line = br. readLine ())! = Null) {if (pw! = Null) pw. println (line); System. out. println (type + ">" + line);} if (pw! = Null) pw. flush ();} catch (IOException ioe) {ioe. printStackTrace ();} finally {try {pw. close (); br. close (); isr. close ();} catch (IOException e) {e. printStackTrace ();}}}}


If you run bat, it will not 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.