无法使用 Runtime.exec() 在 Android Java 代码中执行 shell 命令“echo”
2022-09-02 21:13:33
我可以用来执行像“”和“”这样的shell命令,它们工作正常。Runtime.exec()
getprop
ls system
但是,当我使用“”,“”或“”时,它不会在stdout中显示它。echo $BOOTCLASSPATH
echo \\$BOOTCLASSPATH
echo HelloWorld
日志显示:
I/AndroidRuntime( 4453): VM exiting with result code -1.
这是我的代码:
try {
java.lang.Process proc = Runtime.getRuntime().exec("echo -e \\$BOOTCLASSPATH");
String line = null;
InputStream stderr = proc.getErrorStream();
InputStreamReader esr = new InputStreamReader (stderr);
BufferedReader ebr = new BufferedReader (esr);
while ( (line = ebr.readLine()) != null )
Log.e("FXN-BOOTCLASSPATH", line);
InputStream stdout = proc.getInputStream();
InputStreamReader osr = new InputStreamReader (stdout);
BufferedReader obr = new BufferedReader (osr);
while ( (line = obr.readLine()) != null )
Log.i("FXN-BOOTCLASSPATH", line);
int exitVal = proc.waitFor();
Log.d("FXN-BOOTCLASSPATH", "getprop exitValue: " + exitVal);
} catch (Exception e) {
e.printStackTrace();
}