【问题标题】:How to get batch cmd output continuously in java application如何在java应用程序中连续获取批处理cmd输出
【发布时间】:2018-10-26 18:13:21
【问题描述】:

我编写了一个程序,它运行一个批处理命令 (tshark) 来捕获 2 个 IP 地址之间的数据包大小(连续)。

我使用RuntimeProcess 运行它并使用process.getOutputStream() 获取返回值并在java 终端中打印它们。

我的问题是打印在两条记录之间暂停(打印 1200 行/停止 10 秒/打印 1200 行)。

你知道如何在java应用程序中连续读取批处理命令的OutputStream吗?

【问题讨论】:

  • 可以加代码吗?
  • 您的意思是bash 命令吗?和tshark ?
  • idan :代码在我的答案中。 Bentaye 这是一个批处理 (Windows) 命令。这是 tshark,你是对的,我更正了我的帖子。

标签: java multithreading process buffer outputstream


【解决方案1】:

首先您要读取InputStream:您从 读取InputStream,然后写入 OutputStreamSee this post

然后使用BufferReader 包装您的OutputStream 并在while 循环中逐行读取。

您还可以捕获错误InputStream。如果您没有看到任何输出,调试可能会很有用。

这里是ping 命令的示例。因为我不知道你的tsharp 命令是什么。

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("ping www.google.com");

String line = null;

BufferedReader inputStreamReader = 
    new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((line = inputStreamReader.readLine()) != null) {
    System.out.println(line);
}

BufferedReader errorStreamReader = 
    new BufferedReader(new InputStreamReader(proc.getErrorStream()));
while ((line = errorStreamReader.readLine()) != null) {
    System.out.println(line);
}

【讨论】:

    【解决方案2】:

    这正是我的代码。 Ping 命令很快结束,所以如果我等待几秒钟,我就会得到结果。 Tshark 捕获网络流量,因此不会结束。

    我的问题是阅读暂停。

    这是我的代码:

        String cmd = "c:\\\"Program Files\"\\Wireshark\\tshark.exe -T fields -e frame.len host"+ ipSrc +" and dst "+ ipDst;
    
        String[] command = {"cmd.exe", "/C", cmd};
    
        try {
            final Process proc = Runtime.getRuntime().exec(command);
            try {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(proc.getInputStream()));
                String line = "";
                try {
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }
                } finally {
                    reader.close();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    

    我也尝试使用线程在终端中写入,但这是同样的问题:

        new Thread() {
                @Override
                public void run() {
                    try {
                        BufferedReader reader = new BufferedReader(
                                new InputStreamReader(
                                        process.getInputStream()));
                        String line = "";
                        try {
                            while ((line = reader.readLine()) != null) {
                                System.out.println(line);
                            }
                        } finally {
                            reader.close();
                        }
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
            }.start();
    

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      • 1970-01-01
      • 2012-01-25
      相关资源
      最近更新 更多