在Java程序中使用ADB pull命令
我无法让 ADB 命令在 Java 中运行。 adb 命令在直接输入到命令行时起作用。但是在 Java 中运行它时,出现错误
Cannot run program "adb -s shell": CreateProcess error=2,file not find
运行 Windows 的正确语法是什么Java 的命令行? adb 命令"adb devices"
在 Java 应用程序中工作。我尝试运行的命令是“adb pull sdcard/Download/symmetri.txt C:/Users/myUsername/Downloads/Sources”,它在命令提示符下运行,但不能在Java 应用程序。我的代码是:
public void FilePush() {
try{
String androidFilePath = "sdcard/Download/symmetri.txt ";
String windowsFilePath = "C:\\Users\\myUsername\\Downloads\\Sources\"";
List<String> cmd = new LinkedList<>();
cmd.add("adb -s shell");
cmd.add("adb");
cmd.add("pull");
cmd.add(androidFilePath);
cmd.add(windowsFilePath);
ProcessBuilder builder = new ProcessBuilder(cmd);
builder.redirectErrorStream(true);
Process p = builder.start();
}
catch (IOException e) {
e.printStackTrace();
}
}
我也尝试过不使用 adb -s shell"
和 with
String androidFilePath = "\"/storage/sdcard0/Download/symmetri.txt\"";
String windowsFilePath = "\"C:\\Users\\myUsername\\Downloads\\Sources\\"";
但遇到了相同的错误
I can't get the ADB command to work in Java. The adb command works when inputting it directly to the command line. But when running it in Java, I get error
Cannot run program "adb -s shell": CreateProcess error=2,file not found
What is the proper syntax for running Windows Command line from Java?
The adb command "adb devices"
works in the Java application. The command I'm trying to run is "adb pull sdcard/Download/symmetri.txt C:/Users/myUsername/Downloads/Sources"
, which works in the command prompt, but not from within the Java application. My code is:
public void FilePush() {
try{
String androidFilePath = "sdcard/Download/symmetri.txt ";
String windowsFilePath = "C:\\Users\\myUsername\\Downloads\\Sources\"";
List<String> cmd = new LinkedList<>();
cmd.add("adb -s shell");
cmd.add("adb");
cmd.add("pull");
cmd.add(androidFilePath);
cmd.add(windowsFilePath);
ProcessBuilder builder = new ProcessBuilder(cmd);
builder.redirectErrorStream(true);
Process p = builder.start();
}
catch (IOException e) {
e.printStackTrace();
}
}
I also tried without adb -s shell"
and with
String androidFilePath = "\"/storage/sdcard0/Download/symmetri.txt\"";
String windowsFilePath = "\"C:\\Users\\myUsername\\Downloads\\Sources\\"";
But got the same error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除该行
,因为您不需要
shell
,而是pull
。Remove the line
as you don't want
shell
butpull
.感谢这里乐于助人的人们,我终于成功了!我首先需要将 adb.exe 作为命令运行,然后才能运行任何 adb 命令。我需要从命令列表中删除
("adb -s shell")
。我试图访问外部存储中的文件而不是需要根访问权限的内部文件(尽管我不知道为什么我可以在命令提示符窗口中访问完全相同的文件而不允许根访问)。如果您想访问外部文件,请使用
adb root
添加 root 访问权限适用于可能面临相同问题的任何人的完整代码:
Thx to the helpful people here, I finally got it to work! I first needed to run adb.exe as a command, before I could run any adb commands. I needed to remove
("adb -s shell")
from my list of commands. I was trying to acces a file in the external storage instead of the internal one which needs root access(Although I have no clue why I can acces the exact same file in a Command prompt window without permitting root acces). If you want to access external files, add root access with
adb root
Full code that works for anyone who might face the same issue :