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.