1

I have an exe triggered from within a java swing application. I am able to kill the exe using "taskkill /PID ProcessID " when running the application from ECLIPSE IDE . But when i try to run the jar file for the swing application through a batch file, the exe doesn't get terminated probably because I am unable to obtain the process ID. I am using windows XP 32 bit. Any help will be greatly appreciated

String sDosCommand = "cmd /c tasklist /FI " + "\"" + "IMAGENAME eq " + sProcessName + "\"" ;
Process process = Runtime.getRuntime().exec(sDosCommand ); 

This code (modified to get process ID of one particular process) gives me the Process ID, which in turn I use in Taskkill command executed similarly

Thanks and regards Arun Raj

4

2 回答 2

1

我假设您正在使用ProcessBuilder来启动您的外部应用程序。如果不是,请考虑使用该类及其start()方法来启动外部应用程序,如 Javadoc 中所述。

一旦你调用start()了,你会得到一个Process,你可以调用它destroy()来杀死外部应用程序。

Process p = new ProcessBuilder("myCommand", "myArg").start();
...
p.destroy(); // this kills the command "myCommand"
于 2012-07-24T11:29:31.130 回答
0

我发现问题出在哪里。我正在从批处理文件中触发应用程序 jar。这批还修改了环境变量(准确地说是路径),这阻止了我执行 TASKKILL 命令。我对批处理文件进行了轻微修改以包含 PATH 环境变量。这解决了我的问题。

感谢大家的帮助。阿伦·拉吉

于 2012-07-25T15:45:50.227 回答