java - Runtime.exec().waitFor() doesn't wait until process is done -


i have code:

file file = new file(path + "\\runfromcode.bat"); file.createnewfile();  printwriter writer = new printwriter(file, "utf-8"); (int = 0; <= max; i++) {     writer.println("@cd " + i);     writer.println(native system commands);     // more things }  writer.close();  process p = runtime.getruntime().exec("cmd /c start " + path + "\\runfromcode.bat"); p.waitfor();  file.delete(); 

what happens file deleted before executed.

is because .bat file contains native system call? how can make deletion after execution of .bat file? (i don't know output of .bat file be, since dynamically changes).

by using start, askingcmd.exe start batch file in background:

process p = runtime.getruntime().exec("cmd /c start " + path + "\\runfromcode.bat"); 

so, process launch java (cmd.exe) returns before background process finished.

remove start command run batch file in foreground - then, waitfor() wait batch file completion:

process p = runtime.getruntime().exec("cmd /c " + path + "\\runfromcode.bat"); 

according op, important have console window available - can done adding /wait parameter, suggested @noofiz. following sscce worked me:

public class command {  public static void main(string[] args) throws java.io.ioexception, interruptedexception {        string path = "c:\\users\\andreas";         process p = runtime.getruntime().exec("cmd /c start /wait " + path + "\\runfromcode.bat");         system.out.println("waiting batch file ...");        p.waitfor();        system.out.println("batch file done.");    } } 

if runfromcode.bat executes exit command, command window automatically closed. otherwise, command window remains open until explicitly exit exit - java process waiting until window closed in either case.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -