0

I'm trying to use ProcessBuilder to start application in cmd.exe, wait for it to finish and then close it. So far I tried:

String[] cmdline=new Stirng{}("cmd.exe","/C","start",application_and_parameters);
ProcessBuilder processBuilder = new ProcessBuilder(cmdline);
Process p = processBuilder.start();
//get error and input streams
int exitVal = p.waitFor();

It opens window as expected, but doesn't close. I tried:

p.destroy()

and to send exit command:

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writer.write("exit");
writer.flush();

but without success, cmd stays. Could anyone suggest a solution?

1 Answers1

0

Remove start , It will execute your process directly without opening command prompt so you will not need to close it manually.Below is you code snippet as an example



String[] cmdline=new String[]{"cmd.exe","/C","notepad.exe"};
                ProcessBuilder processBuilder = new ProcessBuilder(cmdline);
                Process p = processBuilder.start();
                //get error and input streams
                int exitVal = p.waitFor();
                System.out.println(exitVal);

Haroon
  • 127
  • 1
  • 6