使用ProcessBuilder清空输出

时间:2014-09-18 01:24:56

标签: java raspberry-pi processbuilder

我尝试使用Java iwlist执行ProcessBuilder命令,但由于某种原因,它会返回一个空输出,而不是我运行时得到的输出在命令行中。

到目前为止,这是我的代码:

ProcessBuilder builder = new ProcessBuilder ("iwlist", "wlan0 scan");
builder.redirectError();
Process process = builder.start();

InputStream stdout = process.getInputStream ();  
BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));

String line;
while ((line = reader.readLine()) != null) {
    System.out.println (">> " + line);
}

但是当我在终端中运行命令时,我得到了:

pi@naath ~ $ sudo iwlist wlan0 scan
wlan0     Scan completed :
          Cell 01 - Address: ...
                    ESSID:"ASDQWE"
                    Protocol:IEEE 802.11bgn
                    Mode:Master
                    Frequency:2.412 GHz (Channel 1)
...

更新

奇怪的是,使用Runtime我可以获得预期的输出,如下所示:

Process p = Runtime.getRuntime().exec("sudo iwlist wlan0 scan");  
BufferedReader in = new BufferedReader(  
                    new InputStreamReader(p.getInputStream()));  
String line = null;  
while ((line = in.readLine()) != null) {  
    System.out.println(line);  
}

0 个答案:

没有答案