获取同步终端输出到java变量

时间:2017-04-21 17:34:51

标签: java linux eclipse terminal

我有这个代码使用java执行终端命令并将输出存储在字符串变量中,但有时输出已更改。如何将连续输出转换为文件或字符串?

当我执行命令时,我得到第一个输出的初始输出 但是在这个示例中,当客户端连接到访问点时输出更改说有人连接了如何将此输出变为变量?

//String output : stores the output of airbase-ng  but after a while the     
//output will change because it will notify me when something happened < so 
//how to store the output again

import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
 class ExecuteShellComand {

    public static void main(String[] args) {

        ExecuteShellComand obj = new ExecuteShellComand();

        String domainName = "google.com";

        //in mac oxs
        String command = "airbase-ng start mon0";

        String output = obj.executeCommand(command);

        System.out.println(output);

            JOptionPane.showMessageDialog(null,output);

    }

 String executeCommand(String command) {

        StringBuffer output = new StringBuffer();

        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader =
                            new BufferedReader(new InputStreamReader(p.getInputStream()));

                        String line = "";
            while ((line = reader.readLine())!= null) {
                output.append(line + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();
    }
}

0 个答案:

没有答案