java

废话不多说,直接上源码,喜欢的朋友可以看一下。

package com.test;

import java.io.IOException;
import java.util.Scanner;

public class JavaWindowsCommandUtil {
??? /**
???? * @param args
???? */?
??? public static void main(String[] args) {?
??????? // TODO Auto-generated method stub?
?
??????? // startTask("E:\\Fetion\\Fetion.exe");?
//??????? killTask("javaw");?
???????
??????? killTask("javaw");
??????? showTaskList();
?????????
??? }?
?
??? /**
???? * 杀死一个进程
???? *?
???? * @param task
???? */?
??? public static void killTask(String task) {?
?
??????? try {?
??????????? Process process = Runtime.getRuntime().exec("taskList");?
??????????? Scanner in = new Scanner(process.getInputStream());?
??????????? int count = 0;?
??????????? while (in.hasNextLine()) {?
??????????????? count++;?
??????????????? String temp = in.nextLine();?
?
??????????????? if (temp.contains(task)) {?
??????????????????? String[] t = temp.split(" ");?
??????????????????? // 判断该进程所占内存是否大于20M?
??????????????????? if (Integer.parseInt(t[t.length - 2].replace(",", "")) > 20000) {?
??????????????????????? temp = temp.replaceAll(" ", "");?
??????????????????????? // 获得pid?
??????????????????????? String pid = temp.substring(9, temp.indexOf("Console"));?
??????????????????????? Runtime.getRuntime().exec("tskill " + pid);?
?
??????????????????????? // dos下开cmd窗口 ntsd -c q -p PID?
??????????????????????? // Runtime.getRuntime().exec("ntsd -c q -p 1528");?
??????????????????? }?
??????????????? }?
??????????????? // System.out.println(count + ":" + temp);?
??????????? }?
??????? } catch (Exception e) {?
??????????? // TODO Auto-generated catch block?
??????????? e.printStackTrace();?
??????? }?
??? }?
?
??? /**
???? * 显示当前机器的所有进程
???? */?
??? public static void showTaskList() {?
?
??????? try {?
??????????? Process process = Runtime.getRuntime().exec("taskList");?
??????????? Scanner in = new Scanner(process.getInputStream());?
??????????? int count = 0;?
??????????? while (in.hasNextLine()) {?
??????????????? count++;?
??????????????? System.out.println(count + ":" + in.nextLine());?
??????????? }?
??????? } catch (Exception e) {?
??????????? // TODO Auto-generated catch block?
??????????? e.printStackTrace();?
??????? }?
??? }?
?
??? /**
???? * 启动一个进程
???? *?
???? * @param task
???? */?
??? public static void startTask(String task) {?
??????? try {?
??????????? Runtime.getRuntime().exec(task);?
?
??????? } catch (IOException e) {?
??????????? // TODO Auto-generated catch block?
??????????? e.printStackTrace();?
??????? }?
??? }?
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。