强制关闭程序进程

package thj1;

import java.awt.AWTException;
import java.awt.Robot;
import java.io.IOException;

public class Command {

	public static void main(String[] args) throws AWTException, IOException {
		// TODO Auto-generated method stub
		shutdownPro("QQ", 5000);// 传入两个参数,一个是想要操作的进程的名字,一个是时间间隔
	}

	public static void shutdownPro(String progressName, int time) throws AWTException, IOException {
		int isRunning = -1;// 定义变量,该变量用来判断某程序是否在运行,初始值为-1
		Robot robot = new Robot();
		while (true) {
			try {
				robot.delay(time);// 使用robot的延时功能来完成每隔多长时间运行一次,还有其他方法实现该功能

				// 下面是用java来调用某个程序的语句,这里定义了一个Window命令,然后去执行。该语句的作用是查看当前在运行的程序
				String cmd = "tasklist";
				Process p = Runtime.getRuntime().exec(cmd);

				// 上述语句执行完后,可以通过Process对象获得窗口返回的数据,把这个数据拼接成一个字符串
				StringBuffer proList = new StringBuffer();
				byte[] b = new byte[1024];
				for (int n; (n = p.getInputStream().read(b)) != -1;) {
					proList.append(new String(b, 0, n));
				}

				isRunning = proList.toString().indexOf(progressName + ".exe");// 调用indexof方法判断某个进程是否存在于我们得到的结果中

				// 如果有,说明在运行
				if (isRunning >= 0) {
					System.out.println("正在关闭");
					String command = "taskkill /f /im " + progressName + ".exe";
					Runtime.getRuntime().exec(command);
					System.out.println("已关闭");
				} else {
					System.out.println("无进程");
				}

			} catch (Exception e1) {
				e1.printStackTrace();
			}

		}
	}

}

猜你喜欢

转载自blog.csdn.net/qq_41547057/article/details/96291217
今日推荐