如何使用Runtime.getRuntime()。exec(“ cmd”) [英] How to use Runtime.getRuntime().exec("cmd")
问题描述
在我的应用程序中,我试图执行SD卡上存在的本机代码。
In my application I'm trying to execute a native code which is present on my SD card.
File sdCard = getExternalFilesDir(null); // directory where native file is placed
String nativeFile = "nativeFile";
String cmd = "shell /system/bin/chmod 0777 " + sdCard.getAbsolutePath() + "/" + nativeFile;
Process proc = Runtime.getRuntime().exec(cmd);
但是只要 Runtime.getRuntime()。exec(cmd)
被执行,它引发错误:
But as soon as Runtime.getRuntime().exec(cmd)
is executed, it throws error:
java.io.IOException: Error running exec(). Command: [shell, /system/bin/chmod, 0777, /storage/emulated/0/Android/data/com.example.andridutilproject/files/native] Working Directory: null Environment: null
任何建议,如何解决?
推荐答案
通常会禁用Android系统中的sdcard以便执行。因此,即使您正确执行chmod命令,它也会失败。
The sdcard in an Android system is usually disabled for execution. Therefore even if you correctly execute the chmod command it will fail.
您可以轻松地进行测试。通过USB启动外壳程序(adb外壳程序)并执行chmod命令。它将失败,并显示诸如错误模式之类的错误消息。
You can test that easily. Start the shell via USB (adb shell) and execute the chmod command. It will fail with an error message like "Bad mode".
因此,您必须将文件复制到具有写访问权的其他位置,然后将可执行位设置为在那个副本上。您可以尝试将文件复制到 / data / local / tmp /中,但是我不确定该路径是否仍然可用。
Therefore you have to copy the file to a different location where you have write access and then set the executable bit on that copy. You can try to copy the file for example to "/data/local/tmp/" but I am not sure if that path is still usable.
这篇关于如何使用Runtime.getRuntime()。exec(“ cmd”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!