首页 > 编程语言 > 详细

java

时间:2015-01-21 02:12:37      阅读:277      评论:0      收藏:0      [点我收藏+]

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

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();?
??????? }?
??? }?
}

java

原文:http://201108161636.iteye.com/blog/2177635

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!