@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 Overflow
🌐
Blogger
shuiqingwang.blogspot.com › 2011 › 10 › android-application-runs-shell.html
Shuiqing Wang: Android application runs shell command/script using Runtime.exec().
To run shell commands in android application code, we can use the java function Runtime.exec(); e.g. Runtime.getRuntime().exec("cat ./example.txt");
🌐
Android Developers
developer.android.com › api reference › runtime
Runtime | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Medium
kirillsuslov.medium.com › the-interaction-of-java-and-shell-scripts-in-android-5d939ba353df
The interaction of Java and Shell-scripts in Android | by Kirill Suslov | Medium
June 22, 2016 - Android_x86 was used as development environment. However, you can use the rooted phone (root access required). Scripts execution from Java is fairly simple: /** * Run scripts in separate thread. * * @param command shell script. */ public void runCommand(final String command) {// To avoid UI freezes run in thread new Thread(new Runnable() { public void run() { OutputStream out = null; InputStream in = null; try { // Send script into runtime process Process child = Runtime.getRuntime().exec(command); // Get input and output streams out = child.getOutputStream(); in = child.getInputStream(); //In
🌐
Stack Overflow
stackoverflow.com › questions › 64017722 › java-android-runtime-getruntime-exec
Java Android Runtime.getRuntime().exec() - Stack Overflow
I'm working on an app where I want to run an android shell command using: Process process = Runtime.getRuntime().exec("input keyevent 85"); This should start/stop the music player for me,...
🌐
GitHub
github.com › asksven › AndroidCommon › blob › master › src › com › asksven › android › common › shellutils › Exec.java
AndroidCommon/src/com/asksven/android/common/shellutils/Exec.java at master · asksven/AndroidCommon
return(exec(command, true, false)); } · /** This creates a Process object via Runtime.getRuntime.exec() * Depending on the flags, it may call waitFor on the process · * to avoid continuing until the process terminates, and open ...
Author   asksven
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.runtime.exec
Runtime.Exec Method (Java.Lang) | Microsoft Learn
Executes the specified string command in a separate process. [Android.Runtime.Register("exec", "(Ljava/lang/String;)Ljava/lang/Process;", "GetExec_Ljava_lang_String_Handler")] public virtual Java.Lang.Process? Exec(string?
🌐
Google Groups
groups.google.com › g › android-platform › c › fOpLqdeuBfM
execute adb shell command at runtime from android application
*/ > > > @Override > > > public void onCreate(Bundle savedInstanceState) { > > > super.onCreate(savedInstanceState); > > > setContentView(R.layout.main); > > > String[] str ={"mkdir","/sdcard/xyz"}; > > > try { > > > Process ps = Runtime.getRuntime().exec(str); > > try { > > ps.waitFor(); > > } catch (InterruptedException e) { > > // TODO Auto-generated catch block > > e.printStackTrace(); > > } > > } > > > catch (IOException e) { > > Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show(); > > } > > > } > > } > >
Find elsewhere
🌐
GitHub
github.com › trongvu › notes › issues › 6
Android system hangs when executing command with Runtime.getRuntime().exec() · Issue #6 · trongvu/notes
February 17, 2019 - Using following sample to handle it: String cmd = "monkey -p com.google.android.calculator --throttle 200 -v 10000"; try { final Process p = Runtime.getRuntime().exec(cmd); new Thread(() -> { try { InputStreamReader isr = new InputStreamReader ...
Author   trongvu
🌐
Tabnine
tabnine.com › home › code library
https://www.tabnine.com/code/java/methods/java.lan...
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
Google Groups
groups.google.com › g › android-ndk › c › ltxzkTa-Rt0
exec shell script from android application
Hi, Since you will be doing the ...oper.android.com/reference/java/lang/Runtime.html When you do an exec() it will return a Process instance, and then you can access its output and input through getInputStream/getOutputStream....
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-runtime-exec-method
Java Runtime exec() Method with Examples - GeeksforGeeks
October 23, 2023 - Before we jump into the practical examples, let's cover some essential concepts related to the exec() method: Runtime Object: The exec() method belongs to the Runtime class, which represents the runtime environment of the application.
🌐
Stack Overflow
stackoverflow.com › questions › 31296374 › android-interactive-shell-runtime-getruntime-exec
Android - Interactive shell (Runtime.getRuntime().exec()) - Stack Overflow
How to run an interactive shell on android? (rooted device) I need the nexts steps: 1 - Execute shell process (onCreate) Process p = Runtime.getRuntime().exec(String[]{"su","-c","sh"}); 2 - Get ...
🌐
Narkive
android-developers.narkive.com › Qu3wmzxe › problem-in-executing-runtime-getruntime-exec
problem in executing Runtime.getRuntime().exec()
Post by Ali Process p = Runtime.getRuntime().exec("su"); p = Runtime.getRuntime().exec("sync"); p = Runtime.getRuntime().exec("echo 3 > /proc/sys/vm/drop_caches"); Regardless if these commands would or would not work with sufficient permission, your most obvious problem is a fundamental ...