本人菜鸟最近在做一个监测方面的东西,根据cpu使用率关闭某个进程,java有哪些方式可以关闭比如ie进程的,求各位高人指点

解决方案 »

  1.   

    Java自己不行。只能借助JNI或者直接用Runtime去执行操作系统指令。
      

  2.   

    try {
                Runtime rt = Runtime.getRuntime();            
                Process pr = rt.exec("cmd /c tskill iexplore");
                int exitVal = pr.waitFor();
                System.out.println("Exited with error code " + exitVal);
            }
            catch (Exception e) {
                System.out.println(e.toString());
                e.printStackTrace();
            }
    怎么调用dos,我的命令哪写的不对,求指导
      

  3.   

    你还需要读取其输出信息,不能只是等待执行而已;pr.getInputStream() 和 pr.getErrorStream()要把里面的内容读取出来,有可能有提示信息或错误信息需要你处理。
      

  4.   

    求代码例子,有关于sigar的最好
      

  5.   

    供你参考吧:import java.util.Scanner;public class ExecuteCMD {    public static void main(String[] args) throws Exception {
            String cmd = "cmd /c dir";
            System.out.println("Executing: " + cmd);
            Process proc = Runtime.getRuntime().exec(cmd); // Executing the Command.
            
            ProcessorReader procReader = new ProcessorReader(proc);
            procReader.start(); // Start the thread for reading process' output
            procReader.join(); // Wait for the thread finish.
            
            System.out.println("Process exit with code: " + proc.exitValue());
        }
    }class ProcessorReader extends Thread {
        Process proc;    public ProcessorReader(Process proc) {
            this.proc = proc;
        }    public void run() {
            Scanner scStdOut = new Scanner(proc.getInputStream(), "GBK"); // Prepare Scanner for standard's output.
            Scanner scErrOut = new Scanner(proc.getInputStream(), "GBK"); // Prepare Scanner for error's output.
            while (true) {
                if (scStdOut.hasNextLine()) {
                    System.out.println(scStdOut.nextLine()); // Standard Output Information
                } else if (scErrOut.hasNextLine()) {
                    System.err.println(scErrOut.nextLine()); // Error Information
                } else {
                    try {
                        Thread.sleep(20); // Nothing to do, sleep a while...
                        proc.exitValue(); // ThrowIllegalThreadStateException, if the subprocess represented by this Process object has not yet terminated.
                        break;
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    } catch (IllegalThreadStateException ex) {
                        // Process still alive
                    }
                }
            }
        }
    }