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?
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.