停止使用批处理文件启动的进程

时间:2011-04-14 04:05:30

标签: java runtime game-engine

我使用

从我的java代码启动一个批处理文件
Runtime.getRuntime().exec("cmd /c start myFile.bat");

反过来启动一个应用程序,现在我想停止这个批处理文件启动的应用程序,如何实现它。可能存在相同应用程序的同一多个实例已在运行的情况,但我想停止此批处理文件启动的实例。是不是可能,我的服务器在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")) ;

但仍然没有运气:(

2 个答案:

答案 0 :(得分:2)

getRuntime()方法返回Process类的对象,并使用Process对象,您可以在Process对象上调用destroy()方法来终止进程

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

答案 1 :(得分: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();