【发布时间】:2017-06-02 00:29:01
【问题描述】:
我可以使用Runtime.exec() 来执行诸如“getprop”和“ls system”之类的shell 命令,它们工作正常。
但是,当我使用“echo $BOOTCLASSPATH”、“echo \\$BOOTCLASSPATH”或“echo HelloWorld”时,它不会在标准输出中显示。
logcat 显示:
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();
}
【问题讨论】:
-
希望this能帮到你。
-
你的系统好像没有/bin/echo; echo 也可用作 shell 命令。尝试执行
sh -c "echo -e \\$BOOTCLASSPATH" -
@user3505725 该链接指的是 Windows 上的问题。伊克。
-
我试过这个:
String[] cmdline = { "sh", "-c", "echo $BOOTCLASSPATH" };Runtime.getRuntime().exec(cmdline);,它可以工作。谢谢你们。 -
感谢好友@QYLin,你救了我的命。你能详细说明为什么会这样吗?
标签: java android shell echo runtime.exec