Solve the Java call to BAT batch not pop up CMD window method analysis

  • 2020-04-01 02:02:58
  • OfStack

Normal invocation :(this will definitely bring up the CMD window)


Runtime.getRuntime().exec("cmd.exe   /C   start   D:\test.bat");

To solve the problem, just add a parameter "/b" after "start" :

Runtime.getRuntime().exec("cmd.exe   /C   start   /b   D:\test.bat");


Runtime rt = Runtime.getRuntime(); 
Process ps = null; 
try {
   ps = rt.exec("cmd.exe /C start /b D:\test.bat");
} catch (IOException e1) {
   e1.printStackTrace();
} 
ps.waitFor(); 
int i = ps.exitValue(); 
if (i == 0) { 
  System.out.println(" completes .") ; 
} else { 
  System.out.println(" On failure .") ;
}


Related articles: