如何在Java中查找和终止运行的Win进程?
我需要一种Java方法来找到一个正在运行的Win进程,从中我知道可执行文件的名称。我想看看它现在是否正在运行,如果我找到它,我需要一种方法来终止进程。您必须调用一些本机代码,因为没有库可以这样做。由于JNI既麻烦又困难,您可以尝试使用JNA(Java本机访问) 您可以使用命令行工具终止诸如和之类的进程 您也可以使用内置的tasklist.exe和taskkill.exe,但它们仅在Windows XP Professional和更高版本上可用(不在家庭版中)如何在Java中查找和终止运行的Win进程?,java,windows,process,Java,Windows,Process,我需要一种Java方法来找到一个正在运行的Win进程,从中我知道可执行文件的名称。我想看看它现在是否正在运行,如果我找到它,我需要一种方法来终止进程。您必须调用一些本机代码,因为没有库可以这样做。由于JNI既麻烦又困难,您可以尝试使用JNA(Java本机访问) 您可以使用命令行工具终止诸如和之类的进程 您也可以使用内置的tasklist.exe和taskkill.exe,但它们仅在Windows XP Professional和更高版本上可用(不在家庭版中) 用于执行程序。您可以使用命令行win
用于执行程序。您可以使用命令行windows工具
tasklist
和taskkill
并使用Runtime.exec()从Java调用它们。
有一个小API提供了所需的功能:
Windows进程库
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);
}
}
下面是一个非常好的方法:
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}")
}
超级卡克斯写的答案中的小变化
private static final String KILL = "taskkill /IMF ";
改为
private static final String KILL = "taskkill /IM ";
/IMF
选项不起作用。它不会杀死记事本。/IM
选项实际起作用时,请使用以下类()。我正在使用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();
}
}
如果taskkill没有结束您的进程,您可能需要添加“/F”参数来强制执行。看起来不错!我不知道这是一个有效的选择。我更新了答案。Thanxminus 1,因为taskkill命令的语法无效。它应该是
“taskkill/F/IM”
@karawson:不,它不是那样工作的。无论是在命令行中还是在Java中,@plaza我很抱歉,您是对的。我在我的工作站上试过了,现在不行了。可能是windows更新或其他更改。我确实在4年前发布了这篇文章。我这么说的唯一原因是它在生产环境中工作。。然而,这是服务器2008。无论如何,我为你编辑了这篇文章。很好的发现,thanx