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

后端 未结 1 1864
粉色の甜心
粉色の甜心 2020-12-09 17:56

I can use Runtime.exec() to execute shell commands like \"getprop\" and \"ls system\" and they work fine.

However, when I use

相关标签:
1条回答
  • 2020-12-09 18:09

    @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);
    
    0 讨论(0)
提交回复
热议问题