我正在尝试在运行时 exec() 中运行以下命令:
Process p;
try {
p = Runtime.getRuntime().exec("arp -a");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (!inputLine.contains("incomplete")) {
found++;
inputLine = cleanResult(inputLine);
storeList(inputLine);
}
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("check",e.getMessage());
}
我得到的错误是:
07-14 12:23:16.548: W/System.err(14272): java.io.IOException: Error running exec(). Command: [arp, -n] Working Directory: null Environment: null
07-14 12:23:16.668: W/System.err(14272): at java.lang.ProcessManager.exec(ProcessManager.java:211)
07-14 12:23:16.668: W/System.err(14272): at java.lang.Runtime.exec(Runtime.java:168)
07-14 12:23:16.668: W/System.err(14272): at java.lang.Runtime.exec(Runtime.java:241)
07-14 12:23:16.668: W/System.err(14272): at java.lang.Runtime.exec(Runtime.java:184)
07-14 12:23:16.678: W/System.err(14272): at com.app.president.FindDevices$8.run(FindDevices.java:225)
07-14 12:23:16.678: W/System.err(14272): at android.os.Handler.handleCallback(Handler.java:725)
07-14 12:23:16.678: W/System.err(14272): at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 12:23:16.678: W/System.err(14272): at android.os.Looper.loop(Looper.java:137)
07-14 12:23:16.678: W/System.err(14272): at android.app.ActivityThread.main(ActivityThread.java:5099)
07-14 12:23:16.678: W/System.err(14272): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 12:23:16.678: W/System.err(14272): at java.lang.reflect.Method.invoke(Method.java:511)
07-14 12:23:16.678: W/System.err(14272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
07-14 12:23:16.678: W/System.err(14272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
07-14 12:23:16.678: W/System.err(14272): at dalvik.system.NativeStart.main(Native Method)
07-14 12:23:16.678: W/System.err(14272): Caused by: java.io.IOException: Permission denied
07-14 12:23:16.678: W/System.err(14272): at java.lang.ProcessManager.exec(Native Method)
07-14 12:23:16.678: W/System.err(14272): at java.lang.ProcessManager.exec(ProcessManager.java:209)
07-14 12:23:16.688: W/System.err(14272): ... 13 more
我检查的日志内容如下:
07-14 12:23:19.971: E/check(14272): Error running exec(). Command: [arp, -a] Working Directory: null Environment: null
这段代码的作用是,它尝试读取 arp 缓存以获取所有 MAC 地址及其各自的 IP 地址。
有人可以帮我解决这个问题并让我的代码运行。我想知道为什么下面的代码会给出这个错误?这样做是否需要任何权限?到目前为止,我授予的权限是:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
提前致谢 :)