欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java,

来源: javaer 分享于  点击 27165 次 点评:58

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

相关文章

    暂无相关文章
相关栏目:

用户点评