Java Execute bat batch file and close cmd window
Import java.io.IOException;
public class Cmdmain {
public static void Main (string[] args) {
Execute batch File
String strcmd= "cmd/c start D:\\antrelease.bat";
Runtime RT = Runtime.getruntime ();
Process PS = null;
try {
PS = rt.exec (strcmd);
catch (IOException E1) {
E1.printstacktrace ();
}
try {
Ps.waitfor ();
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
int i = Ps.exitvalue ();
if (i = = 0) {
System.out.println ("execution completed.");
} else {
System.out.println ("Execution failed.");
}
Ps.destroy ();
PS = null;
Batch processing after execution, according to the Cmd.exe process name kill the cmd window (this method is very hard to find, online many introduced are invalid, csdn waste me 3 points to find this method)
New Cmdmain (). KillProcess ();
}
public void KillProcess () {
Runtime RT = Runtime.getruntime ();
Process p = null;
try {
Rt.exec ("cmd.exe/c start WMIC process where name= ' cmd.exe ' call terminate");
catch (IOException e) {
E.printstacktrace ();
}
}
}