我尝试使用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);
}