Executing a Command from Java and Waiting for the Command to Finish

前端 未结 3 1002
粉色の甜心
粉色の甜心 2020-12-30 06:26

In my Java program, I create a process that executes a command to run a batch file like this:

try {
        File tempFile = new File(\"C:/Users/Public/temp.c         


        
相关标签:
3条回答
  • 2020-12-30 06:51

    The answer given is correct. I added that the window opened by the code needs to be closed manually.

    Process p = Runtime.getRuntime().exec("cmd /C start /wait filepath.bat");
    int exitVal = p.waitFor();
    
    0 讨论(0)
  • 2020-12-30 06:55

    I manged to find the answer elsewhere. To keep the initial process open until the batch file finished all you need is "/wait"

    Process p = Runtime.getRuntime().exec("cmd /C start /wait filepath.bat");
    int exitVal = p.waitFor();
    
    0 讨论(0)
  • 2020-12-30 07:09

    calling "cmd /c start" causes cmd to fire off another instance and exit immediately. Try taking out the "start" command.

    0 讨论(0)
提交回复
热议问题