2

我遇到了这个问题。我的程序在 Windows 平台上
调用。Runtime.getRuntime().exec(cmd);我读取了错误和输出流并对这个输出做一些事情。此方法每 4-5 秒循环调用一次,一直持续到程序终止。

现在会发生什么,每次我读取输出时,前一个输出都会附加到新输出中,因此每次迭代结果都会变得越来越大。有没有办法阻止这件事。执行的命令是带有一些过滤参数的“tasklist”。

我为此 Runtime.getTuntime().exec(cmd) 创建了一个方法(返回字符串输出),在该方法中,我也在执行后关闭进程,但是当从循环内调用它时,每次将先前的输出附加到新的那一个。

这是代码:

class Track implements Runnable {

static int size = 0;

public void run() {

    String cmd1 = "tasklist /fo list /fi \"imagename eq java.exe\"";
    String cmd2 = "tasklist /fo list /fi \"imagename eq javaw.exe\"";
    String text = "";
    int i=1, j=0;

    while(size < 100000){

        try{
            text = fList.pList(cmd2, 1);
            if (text.indexOf("javaw.exe")== -1){
                text = fList.pList(cmd1, 1);
            }
            if(j==22) System.out.println(text);
            if (text.charAt(0)!= '0') continue;
            i = text.lastIndexOf("Mem Usage:    ")+14;
            text = text.substring(i);
            text = text.substring(0,text.lastIndexOf(" K"));
            text = text.replaceFirst(",", "");
            size = Integer.parseInt(text);
            System.out.println(size);
            Thread.sleep(3000);
            j++;

        } catch(Exception e){
            System.out.println(e);
        }
    }
    System.out.println("Memory utlization exceeded the permissible limit");
    System.out.println("Now terminating the Program\n");
    System.exit(1);


}

static void memoryCheck(int size) throws Exception{

    (new Thread(new Track())).start();

}

}  

在类 fList 中是方法 pList:

static String pList(String cmd, int eval) throws Exception{ //can execute external command

    String out = "";
    int val = 5; // should not be zero, to verify returned code zero for normal exec.
    try
    {            
        //String osName = System.getProperty("os.name" );

        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(cmd);
        // any error message?
        eProcList error = new eProcList(proc.getErrorStream());            

        // any output?
        eProcList output = new eProcList(proc.getInputStream());

        // kick them off
        error.start();
        output.start();


        // any error???
        int exitVal = proc.waitFor();
        out = eProcList.procList();
        val = exitVal;        
        proc.destroy();
        proc.getInputStream().close();
        proc.getErrorStream().close();

    } catch (Throwable t)
    {
        t.printStackTrace();
    }
    if (eval==1) return val + out;
        return out;
}

class eProcList extends Thread
{
    InputStream iStream;
    static String oPut = "";
    eProcList(InputStream iStream)
    {
        this.iStream = iStream;
    }

    public void run()
    {
        try
        {
            InputStreamReader isr = new InputStreamReader(iStream);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
                oPut = oPut + line+"\n";
            } catch (IOException e)
              {
                e.printStackTrace();  
              }
    }

    static public String procList(){
        return oPut;
    }
}

你问了,所以我把所有的都复制到这里了。

4

1 回答 1

1

您创建了oPut一个static字段 - 它在加载类时初始化为""一次,然后在每个新实例之间共享eProcList,即从未清除上一次运行。要么不做static(为什么它是静态的?),要么在构造函数中清除它。

于 2010-12-23T05:16:06.540 回答