2014-03-07 71 views
0

我試圖使用ProcessBuilderCMD.EXE啓動應用程序,等待它完成,然後將其關閉。到目前爲止,我嘗試過:打開和關閉應用從Java

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(); 

它按預期打開窗口,但未關閉。我想:

p.destroy() 

併發送退出命令:

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

但沒有成功,CMD住宿。任何人都可以提出解決方案

+1

試試這個:HTTP:/ /stackoverflow.com/questions/3468987/executing-another-application-from-java –

回答

0

刪除開始,它會直接執行的過程,而無需打開命令提示符,所以你不會需要關閉它manually.Below是你的代碼片段作爲一個例子

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);