我需要一种 Java 方法来找到一个正在运行的 Win 进程,我从中知道可执行文件的名称。我想看看它现在是否正在运行,如果我找到它,我需要一种方法来终止该进程。
GHad
问问题
73868 次
8 回答
46
private static final String TASKLIST = "tasklist";
private static final String KILL = "taskkill /F /IM ";
public static boolean isProcessRunning(String serviceName) throws Exception {
Process p = Runtime.getRuntime().exec(TASKLIST);
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
if (line.contains(serviceName)) {
return true;
}
}
return false;
}
public static void killProcess(String serviceName) throws Exception {
Runtime.getRuntime().exec(KILL + serviceName);
}
例子:
public static void main(String args[]) throws Exception {
String processName = "WINWORD.EXE";
//System.out.print(isProcessRunning(processName));
if (isProcessRunning(processName)) {
killProcess(processName);
}
}
于 2011-09-19T15:44:31.220 回答
16
您可以使用命令行窗口工具tasklist
,taskkill
并使用Runtime.exec()
.
于 2008-09-17T11:06:18.833 回答
3
于 2010-04-19T13:59:56.177 回答
3
这是一种很时髦的做法:
final Process jpsProcess = "cmd /c jps".execute()
final BufferedReader reader = new BufferedReader(new InputStreamReader(jpsProcess.getInputStream()));
def jarFileName = "FileName.jar"
def processId = null
reader.eachLine {
if (it.contains(jarFileName)) {
def args = it.split(" ")
if (processId != null) {
throw new IllegalStateException("Multiple processes found executing ${jarFileName} ids: ${processId} and ${args[0]}")
} else {
processId = args[0]
}
}
}
if (processId != null) {
def killCommand = "cmd /c TASKKILL /F /PID ${processId}"
def killProcess = killCommand.execute()
def stdout = new StringBuilder()
def stderr = new StringBuilder()
killProcess.consumeProcessOutput(stdout, stderr)
println(killCommand)
def errorOutput = stderr.toString()
if (!errorOutput.empty) {
println(errorOutput)
}
def stdOutput = stdout.toString()
if (!stdOutput.empty) {
println(stdOutput)
}
killProcess.waitFor()
} else {
System.err.println("Could not find process for jar ${jarFileName}")
}
于 2015-04-02T12:54:32.280 回答
2
您可以使用命令行工具来杀死SysInternals PsKill和SysInternals PsList等进程。
您也可以使用内置的 tasklist.exe 和 taskkill.exe,但这些仅适用于 Windows XP Professional 及更高版本(不适用于家庭版)。
使用java.lang.Runtime.exec执行程序。
于 2008-09-17T10:53:58.473 回答
1
使用以下类杀死 Windows 进程(如果它正在运行)。我正在使用 force 命令行参数/F
来确保该/IM
参数指定的进程将被终止。
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class WindowsProcess
{
private String processName;
public WindowsProcess(String processName)
{
this.processName = processName;
}
public void kill() throws Exception
{
if (isRunning())
{
getRuntime().exec("taskkill /F /IM " + processName);
}
}
private boolean isRunning() throws Exception
{
Process listTasksProcess = getRuntime().exec("tasklist");
BufferedReader tasksListReader = new BufferedReader(
new InputStreamReader(listTasksProcess.getInputStream()));
String tasksLine;
while ((tasksLine = tasksListReader.readLine()) != null)
{
if (tasksLine.contains(processName))
{
return true;
}
}
return false;
}
private Runtime getRuntime()
{
return Runtime.getRuntime();
}
}
于 2015-06-15T11:10:38.487 回答
0
您将不得不调用一些本机代码,因为恕我直言,没有库可以做到这一点。由于 JNI 既麻烦又困难,您可以尝试使用 JNA(Java Native Access)。https://jna.dev.java.net/
于 2008-09-17T10:48:12.873 回答
0
Super kakes 写的答案的微小变化
private static final String KILL = "taskkill /IMF ";
变成 ..
private static final String KILL = "taskkill /IM ";
/IMF
选项不起作用.它不会杀死记事本..而/IM
选项实际上有效
于 2015-05-26T12:17:53.947 回答