检测进程是否正在运行并杀死它?
问题描述:
嗨.
首先,我必须声明这不适合恶意使用.
我对iTunes感到厌烦,所以我想编写一个程序来检测它的运行并杀死它.
Hi.
First of all, i have to state that this is not intended for malicius use.
I am fed up with iTunes, so i want to write a program that detects it running and kills it.
How do you do that in Java?
答
您可以尝试调用外部进程来完成这项工作,就像这样;
首先是一个名为StreamEater
;
的帮助器类
You could try calling external processes to do the job, like this;
First a helper class calledStreamEater
;
package my.company;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class StreamEater extends Thread
{
private final InputStream is;
private final StringBuilder result = new StringBuilder();
private boolean failed = false;
private Exception exception;
StreamEater(final InputStream is)
{
this.is = is;
}
public String getResult() {
return result.toString();
}
public boolean hasFailed() {
return failed;
}
public Exception getException() {
return exception;
}
public void run()
{
try
{
final InputStreamReader isr = new InputStreamReader(is);
final BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
result.append(line + "\n");
}
}
catch (Exception e)
{
exception = e;
failed = true;
}
}
}
然后有一个看起来像这样的应用程序;
And then have an application that looks something like this;
package my.company;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Program {
private static String executeCommand(String[] command) throws IOException, InterruptedException {
final Process process = Runtime.getRuntime().exec(command);
final StreamEater error = new StreamEater(process.getErrorStream());
final StreamEater output = new StreamEater(process.getInputStream());
error.start();
output.start();
final int exitCode = process.waitFor();
// Check errors
if (error.hasFailed())
System.err.println("Encountered problems reading error stream; " + error.getException().getMessage());
else {
if (error.getResult().length() > 0) {
System.err.println("Error output:");
System.err.println(error.getResult());
}
}
// Check output
if (output.hasFailed()) {
System.err.println("Encountered problems reading output stream; " + output.getException().getMessage());
}
return output.getResult();
}
private static String executeCommand(String command) throws IOException, InterruptedException {
return executeCommand(new String[] { command });
}
public static List<integer> getPids(String exeName) throws IOException, InterruptedException {
String result = executeCommand("tasklist");
StringTokenizer tokenizer = new StringTokenizer(result, "\r\n");
List<integer> pids = new ArrayList<integer>();
while(tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken();
if (line.startsWith(exeName)) {
StringTokenizer spaceTokenizer = new StringTokenizer(line, " ");
if (spaceTokenizer.hasMoreTokens()){
spaceTokenizer.nextToken();
if (spaceTokenizer.hasMoreTokens()){
pids.add(Integer.parseInt(spaceTokenizer.nextToken()));
}
}
}
}
return pids;
}
public static void killProcess(int pid) throws IOException {
Runtime.getRuntime().exec("taskkill /F /PID " + pid);
}
public static void main(String[] args) throws Exception {
List<integer> pids = getPids("notepad.exe");
for(int pid : pids) {
System.out.println(pid);
killProcess(pid);
}
}
}</integer></integer></integer></integer>
希望这会有所帮助,
弗雷德里克(Fredrik)
Hope this helps,
Fredrik