[英]Run adb shell commands from Java code on different platforms
I am using Mac to run Java program which contains some commands to execute on a remote Android device.我正在使用 Mac 运行 Java 程序,其中包含一些要在远程 Android 设备上执行的命令。 When I execute my program on a Windows machine it gives proper output but when I execute the same program on a Mac machine it doesn't.
当我在 Windows 机器上执行我的程序时,它会提供正确的输出,但是当我在 Mac 机器上执行相同的程序时,它不会。 Here I am sharing the code snippet and the output I get.
我在这里分享代码片段和我得到的输出。
Code:代码:
private static final String DUMPSYSCOMMAND = "adb -s ? shell \"dumpsys package com.PACKAGENAME.service | grep versionName\"";
String versionString = runADBCommand(DUMPSYSCOMMAND.replace("?",
deviceIP));
System.out.println("Version String Result " + versionString);
String versionName = null;
if (versionString != null && !versionString.isEmpty()) {
versionString = versionString.replace("\\s+", "");
versionName = versionString.replace(".", "-")
.substring(versionString.indexOf("=") + 1)
.replaceAll("\\s+", "");
System.out.println("Version String " + versionName);
}
public String runADBCommand(String adbCommand) throws IOException {
System.out.println("Running given command= " + adbCommand + "$$$");
StringBuffer returnValue = new StringBuffer();
String line;
InputStream inStream = null;
try {
System.out.println("adbCommand = " + adbCommand);
Process process = Runtime.getRuntime().exec(adbCommand);
// process.waitFor();/
inStream = process.getInputStream();
BufferedReader brCleanUp = new BufferedReader(
new InputStreamReader(inStream));
while ((line = brCleanUp.readLine()) != null) {
if (!line.equals("")) {
System.out.println("After exec");
System.out.println("Line=" + line);
}
// returnValue = returnValue + line + "\n";
returnValue.append(line).append("\n");
}
brCleanUp.close();
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error: " + e.getMessage());
}
System.out.println(returnValue.toString() + "@@");
return returnValue.toString();
}
OutPut:输出:
@@Running given command= adb -s DEVICEIP shell "dumpsys package PACKAGENAME | grep versionName"$$$
adbCommand = adb -s DEVICEIP shell "dumpsys package PACKAGENAME | grep versionName"
After exec
Line=/system/bin/sh: dumpsys package PACKAGENAME | grep versionName: not found
/system/bin/sh: dumpsys package PACKAGENAME | grep versionName: not found
@@Version String Result /system/bin/sh: dumpsys package PACKAGENAME | grep versionName: not found
Version String /system/bin/sh:dumpsyspackagecom-PACKAGENAME|grepversionName:notfound
When I run the same shell command from the command prompt it gives me the expected output on Mac as well.当我从命令提示符运行相同的 shell 命令时,它也会为我提供 Mac 上的预期输出。
Better use ProcessBuilder
instead.最好改用
ProcessBuilder
。 But if you insist on using Runtime.getRuntime().exec()
- use .exec(String[] cmdarray)
instead of your current .exec(String command)
:但是如果你坚持使用
Runtime.getRuntime().exec()
- 使用.exec(String[] cmdarray)
而不是你当前的.exec(String command)
:
private static final String DUMPSYSCOMMAND = "dumpsys package com.PACKAGENAME.service | grep versionName";
String versionString = runADBCommand({"adb", "-s", deviceIP, "shell", DUMPSYSCOMMAND});
...
public String runADBCommand(String[] adbCommand) throws IOException {
...
// do not forget to remove / modify this println - it expect a string
// System.out.println("adbCommand = " + adbCommand);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.