Execute a cmd command from a Java program

1

Hi, I am trying to run a .bat from java, that has a command and must create a file with the output, the .bat works correctly, the problem I have when invoking it from the java program since it does not wait for it to finish and my file is created without content. I have also tried to execute the command directly if the .bat and the same thing happens to me. I'm trying this, but it does not work, any ideas? thank you :)  Process p = Runtime.getRuntime (). Exec (cmd);

        try {
            p.waitFor();

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
asked by Silvia 18.02.2018 в 12:03
source

1 answer

0

You need to use / wait to keep the process open until the .bat runs out

Process process = Runtime.getRuntime().exec("cmd /C start /wait path.bat");
    
answered by 19.02.2018 / 16:38
source