Android:运行时exec()给出了null环境?

时间:2014-07-14 07:10:14

标签: java android android-permissions android-runtime

我试图在Runtime 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缓存以获取具有各自IP地址的所有MAC地址。

有人可以帮助我,让我的代码运行。我想知道为什么以下代码会出现此错误?这样做是否需要任何权限?我迄今给予的许可是:

<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" />

提前致谢:)

1 个答案:

答案 0 :(得分:2)

添加此权限:

android.permission.WRITE_EXTERNAL_STORAGE