@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);
Answer from QY Lin on Stack OverflowFirst, you should wrap calls to exec in a try-catch-clause to catch IOExceptions.
Second, use exec(java.lang.String[]) to execute a command with parameters. For example, similar to
Runtime.getRuntime().exec(new String[]{ "shell", "/system/bin/chmod", "0777", sdCard.getAbsolutePath() + "/" + nativeFile });
The sdcard in an Android system is usually disabled for execution. Therefore even if you correctly execute the chmod command it will fail.
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".
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.