Unable using Runtime.exec() to execute shell command “echo” in Android Java code

断了今生、忘了曾经 提交于 2019-11-29 14:07:57
QY Lin

@Adi Tiwari, I've found the cause. Runtime.getRuntime.exec() doesn't execute a shell command directly, it executes an executable with arguments. "echo" is a builtin shell command. It is actually a part of the argument of the executable sh with the option -c. Commands like ls are actual executables. You can use type echo and type ls command in adb shell to see the difference.
So final code is:

String[] cmdline = { "sh", "-c", "echo $BOOTCLASSPATH" }; 
Runtime.getRuntime().exec(cmdline);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!