【问题标题】:Runtime.getRuntime().exec(cmd); appending previous output with each new execution?Runtime.getRuntime().exec(cmd);在每次新执行时附加先前的输出?
【发布时间】:2010-12-23 04:13:28
【问题描述】:

我遇到了这个问题。
我的程序在 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;
    }
}

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

【问题讨论】:

  • 您应该发布负责读取输出的代码。
  • Java 类名应以大写字母LikeThis 开头。这是一种让代码更易于阅读的约定。

标签: java runtime.exec


【解决方案1】:

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

【讨论】:

  • 我不得不这样做,因为它给出了编译时错误。 java.lang.Error:未解决的编译问题:无法对非静态字段 oPut 进行静态引用
  • @Sara - 从oPutpublic String procList() 中删除静态限定符。然后阅读staticdownload.oracle.com/javase/tutorial/java/javaOO/classvars.html
  • 非常感谢。你解决了这个问题。我总是忽略关注那些变量类型并且主要使用静态,现在我意识到它的问题。再次感谢!!实际上,除了删除静态限定符之外,我还必须将 eProcList.procList 更改为 out = output.procList()+error.procList();
  • @Sara - 乐于助人。不要忘记通过投票和接受答案来成为一名优秀的 StackOverflow 网友。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
相关资源
最近更新 更多