如何使用java关闭外部应用程序
Possible Duplicate:
Knowing which java.exe process to kill on a windows machine
How to kill a process in Java, given a specific PID
我试图找出如何关闭一个特定的外部exe,即cwserv5。exe。我已成功启动新的外部exe并将其关闭。但这不是一个现有的过程。你能帮忙吗?下面是我正在修补的东西,但老实说,我真的丢失了
package com.TestCase;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ReStartEXE {
static Process pr;
public static void open() {
//ProcessBuilder
try {
Runtime rt = Runtime.getRuntime();
//Process pr = rt.exec("cmd /c dir");
pr = rt.exec("C:\\APPLEGREEN\\webserv\\cwserv5rost.exe");
Thread.sleep(10000);
//pr.wait(10000);
//pr.waitFor();
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
// String line=null;
/*while((line=input.readLine()) != null) {
System.out.println(line);
}*/
//int exitVal = pr.waitFor();
//pr.destroy();
// Process.kill(pr);
// Runtime.getRuntime().exec("taskkill /F /IM cwserv5rost.exe");
//System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
public static void Close() {
pr.destroy();
}
}
# 1 楼答案
试试这个
# 2 楼答案
你可以通过执行一个进程来关闭你想要关闭的进程
在你的评论中,我可以看到
taskkill
,这意味着你可能正在使用Windows这将强制映像名为
cwserv5.exe
的进程结束如果不想强制结束,请不要使用
/f
标记有关
taskkill
的更多信息,请转到cmd
(命令提示符)并键入taskkill /?