我需要一种Java方法来查找正在运行的Win进程,我知道可执行文件的名称。我想看看它是否正在运行,如果我发现它,我需要一种方法来终止它。
答案 0 :(得分:39)
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);
}
}
答案 1 :(得分:15)
您可以使用命令行窗口工具tasklist
和taskkill
,并使用Runtime.exec()
从Java调用它们。
答案 2 :(得分:3)
答案 3 :(得分:2)
您可以使用命令行工具来终止SysInternals PsKill和SysInternals PsList等过程。
您也可以使用内置tasklist.exe和taskkill.exe,但这些只能在Windows XP Professional及更高版本(不在家庭版中)上使用。
使用java.lang.Runtime.exec执行程序。
答案 4 :(得分:2)
这是一种时髦的方式:
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}")
}
答案 5 :(得分:1)
将以下课程用于kill a Windows process(if it is running)。我使用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();
}
}
答案 6 :(得分:0)
你必须调用一些本机代码,因为恕我直言,没有库可以做到这一点。由于JNI很麻烦,你可能会尝试使用JNA(Java Native Access)。 https://jna.dev.java.net/
答案 7 :(得分:0)
Super kakes写的答案变化很小
private static final String KILL = "taskkill /IMF ";
已更改为..
private static final String KILL = "taskkill /IM ";
/IMF
选项不起作用。它不会杀死记事本..而/IM
选项实际上有效