cmd netstat通过java无法运行

时间:2016-12-05 08:57:59

标签: java cmd netstat

我想在java上计算已建立的连接,我在cmd上运行以下命令(windows)并得到正确的输出:

netstat -an | find /c "EST"

但是当我试图通过java运行这个命令时我不能。

我的代码:

        String executed = "netstat -an | find \"EST\" ";
        System.out.println(executed);
        Process p = Runtime.getRuntime().exec(executed);

        String s = null;
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

        // read the output from the command
        System.out.println("Here is the standard output of the command:\n");
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }

        // read any errors from the attempted command
        System.out.println("Here is the standard error of the command (if any):\n");
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }
        p.waitFor();
        System.out.println("DONE");

我的输出:

enter image description here

0 个答案:

没有答案