@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
🌐
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 · 中文 – 简体
🌐
Google Support
support.google.com › android › thread › 46167428
Cannot start binary using Runtime.getRuntime().exec() - Android Community
May 12, 2020 - Skip to main content · Android Help · Sign in · Google Help · Help Center · Community · Android · Terms of Service · Submit feedback · Send feedback on
🌐
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?
🌐
GitHub
github.com › trongvu › notes › issues › 6
Android system hangs when executing command with Runtime.getRuntime().exec() · Issue #6 · trongvu/notes
February 17, 2019 - 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 (p.getInputStream()); BufferedReader br = new BufferedReader(isr); while (true) { String s = br.readLine (); if (s == null) break; System.out.println (s); } p.getInputStream().close (); } catch (Exception ex) { System.out.println ("Problem reading stream getInputStream: " + ex); ex.printStackTrace (); } }).run(); new Thread(() -> { try { InputStreamReader isr = new InputStreamReader (p.getE
Author   trongvu
🌐
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,...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 22244143 › how-to-use-runtime-getruntime-execcmd-for-a-long-command-separated-by
android - How to use Runtime.getRuntime().exec(cmd) for a long command separated by ; - Stack Overflow
private void call( String cmd) { Process ans_call; InputStreamReader cmd_reader; BufferedReader cmd_bufferedReader; String line = null; try { sendMainUIMessage(MSG_KPI_UI_UPDATE_REQUEST, null, cmd); mLogWriter.writeDiagnoseLog(cmd); ans_call = Runtime.getRuntime().exec(cmd); NotifyRunning(ans_call); cmd_reader = new InputStreamReader(ans_call.getInputStream()); cmd_bufferedReader = new BufferedReader(cmd_reader); while (((line = cmd_bufferedReader.readLine()) != null)) { sendMainUIMessage(MSG_KPI_UI_UPDATE_REQUEST, null, line + "\n"); mLogWriter.writeDiagnoseLog(line); } //end of while ans_call.waitFor(); NotifyEnd(ans_call); cmd_reader.close(); cmd_bufferedReader.close(); } catch (IOException e) { Log.e(TAG, "Could not write file " + e.getMessage()); } catch (InterruptedException e) { Log.e(TAG, "Ping test Fail: InterruptedException"); } //end of try }
🌐
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
Log.d("Exec.exec", "Executing command " + command); Process p = Runtime.getRuntime().exec(command); · // Print the output. Since we read until there is no more · // input, this causes us to wait until the process is · // completed.
Author   asksven
🌐
Stack Overflow
stackoverflow.com › questions › 43914035 › how-to-get-the-result-of-runtime-getruntime-exec-directly
android - How to get the result of Runtime.getRuntime().exec directly - Stack Overflow
May 11, 2017 - I'm working on a rooted android device. I'm trying to capture the screen and store the result in Bitmap for later usage. String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath(); path += "/img.png"; Process sh = Runtime.getRuntime().exec("su", null,null); OutputStream os = sh.getOutputStream(); os.write(("/system/bin/screencap -p " + path).getBytes("ASCII")); os.flush(); os.close(); sh.waitFor(); final Bitmap x = BitmapFactory.decodeFile(path); What I'm doing here is naming a path for a new image and capturing the screen using the command /system/bin/screencap -p FILEPATH.
🌐
Narkive
android-developers.narkive.com › Qu3wmzxe › problem-in-executing-runtime-getruntime-exec
problem in executing Runtime.getRuntime().exec()
To post to this group, send email to android-***@googlegroups.com To unsubscribe from this group, send email to android-developers+***@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en ... 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 misunderstanding of the "su" command sometimes available on custom roms, and on unix-like systems in general.