Java 프로그램을 작성하여 장치를 찾고 해당 내용을 나열하려고합니다. 이를 위해 먼저 Java의 Process 클래스를 통해 "adb 장치"를 실행하고 출력을 구문 분석하여 finde 만 작동합니다. 그런 다음이 명령을 발사 할 : 내 터미널에 난 오른쪽 출력,/sdcard에 /의 루트에있는 모든 폴더와 파일의 목록을 얻을 넣어Java ADB 셸이 아무 것도 반환하지 않습니다.
adb shell -s DEVICE-SERIAL /sdcard/
.
Java 코드에 넣으면 아무것도 반환되지 않습니다. 그러나 Stderr에는 adb 명령의 철자를 잘못 입력했을 때 나타나는 표준 도움말 페이지가 있습니다. 다음은 재미있는 일입니다. 디버깅을 위해 ADB로 보낸 모든 명령을 출력합니다. 내 programm을 실행하고 정확히 동일한 생성 된 명령을 복사하여 터미널에서 실행하면 정상적으로 작동합니다.
왜 그럴까요? 참고로, 여기 내 코드 발췌은위한 것입니다 :
ADB.java :
public static String runShellCommand(Device d, String command) {
return runCommand(d, "shell " + command);
}
public static String runCommand(Device d, String command) {
Runtime runtime = Runtime.getRuntime();
String full = "";
String error = "";
try {
if (d != null) {
command = "-s " + d.getSerial() + " " + command;
}
System.out.println("Running command: adb " + command);
Process process = runtime.exec(new String[] {ADB_LOCATION, command});
process.waitFor();
BufferedReader stdin = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader errin = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = stdin.readLine()) != null) {
full += line;
}
while ((line = errin.readLine()) != null) {
error += line;
}
if (!error.isEmpty()) {
System.err.println("\n=== ADB reports: ===");
System.err.println(error);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println("App was overenthuasiastic. Couldn't wait for thread to finish.");
}
return full;
}
Device.java :
public ArrayList<Folder> getFoldersInDirectory(String dir) {
String[] list = ls(dir);
for (String s: list) {
System.out.println(s);
System.out.println(isFolderOrFile(dir + s));
}
// Just for debugging, gets completed once this runs.
return null;
}
protected String[] ls(String dir) {
String result = ADB.runShellCommand(this, "ls " + dir);
System.out.println(result);
return result.split("\n");
}
는 내가 전에 말했듯이, 다른 모든 명령이 실행 그냥 괜찮아 (나는 장치를 테스트, get-state, get-serialno).
출력 : runtime.exec()
에 대한
없는 –
runShellCommand 기본적으로 그냥 runCommand가 호출하고 전에 "쉘"을두고하지만 난 게시합니다 니펫 그것. –