2011-04-14 152 views
2

我使用停止使用批處理文件

Runtime.getRuntime().exec("cmd /c start myFile.bat"); 

從而啓動一個應用程序,現在我想停止應用程序被這個批處理文件,如何開始了我的java代碼啓動批處理文件進程中啓動實現這一目標。有可能是同一個應用程序的同一個多個實例已經在運行,但是我想停止這個批處理文件啓動的那個實例。有可能,我的服務器在windows上。

,因爲這是遊戲應用程序和所需要的環境也試過

Runtime rt = Runtime.getRuntime() ; 
      Process p = rt.exec("C:/Valve/HLServer/hlds.exe +maxplayers 32 -game cstrike -console +port 27015 -nojoy -noipx -heapsize 250000 +map cs_italy +servercfgfile server.cfg +lservercfgfile +mapcyclefile mapcycle.txt +motdfile motd.txt +logsdir logs -zone 2048",null, new File("C:/Valve/HLServer")) ; 

但仍沒有運氣:(

+0

你有什麼myFile.bat做了什麼? – 2011-04-14 04:10:32

+0

批處理文件轉到所需的目錄,並從目錄中啓動一個exe文件 – Varun 2011-04-14 04:44:24

回答

1
this will kill the command prompt not the method launched from that command prompt. 

他是正確的

做到這一點

Process process = Runtime.getRuntime().exec ("/folder/exec.exe") //your application 
p.destroy(); 
+0

這不適用於我的這種應用程序。它會從Steam啓動一個遊戲HLDS,必須以控制檯模式啓動,並且必須傳遞一些參數給它。 – Varun 2011-04-14 04:59:52

+0

你可以傳遞參數,就像你在批處理文件中一樣 – 2011-04-14 05:07:32

+0

@varun在這種情況下,你可以傳遞像「exec.exe parameter1 parameter2 parameter3」這樣的參數 – Searock 2011-04-14 05:13:19

2

getRuntime()方法返回Process類的對象,並使用Process對象,你可以調用destroy()方法上處理對象以殺死進程

Process p = Runtime.getRuntime().exec("cmd /c start myFile.bat"); //starts the process 
p.destroy(); //kills the process 
+0

+1我從未注意到銷燬方法。令人敬畏的d (^_^) b – 2011-04-14 04:23:25

+2

這將殺死命令提示符,而不是從該命令提示符啓動的方法。 – Varun 2011-04-14 04:43:27

+0

@varun那麼不要通過'cmd'直接傳遞'myFile.bat' – Searock 2011-04-14 05:07:59