简体   繁体   English

在Java程序中使用adb pull命令

[英]Using ADB pull command in a Java program

I can't get the ADB command to work in Java. The adb command works when inputting it directly to the command line.我无法在 Java 中使用 ADB 命令。直接在命令行中输入 adb 命令即可。 But when running it in Java, I get error但是在 Java 中运行时,出现错误

Cannot run program "adb -s shell": CreateProcess error=2,file not found

What is the proper syntax for running Windows Command line from Java?从 Java 运行 Windows 命令行的正确语法是什么? The adb command "adb devices" works in the Java application. adb 命令"adb devices"在 Java 应用程序中工作。 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.我尝试运行的命令是"adb pull sdcard/Download/symmetri.txt C:/Users/myUsername/Downloads/Sources" ,它在命令提示符下有效,但在 Java 应用程序中无效。 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我也试过没有adb -s shell"

String androidFilePath = "\"/storage/sdcard0/Download/symmetri.txt\"";
String windowsFilePath = "\"C:\\Users\\myUsername\\Downloads\\Sources\\"";

But got the same error但是得到了同样的错误

Remove the line删除线

cmd.add("adb -s shell");

as you don't want shell but pull .因为你不想要shellpull .

Thx to the helpful people here, I finally got it to work.感谢这里的乐于助人的人,我终于让它工作了。 I first needed to run adb,exe as a command.我首先需要将 adb,exe 作为命令运行。 before I could run any adb commands.在我可以运行任何 adb 命令之前。 I needed to remove ("adb -s shell") from my list of commands.我需要从命令列表中删除("adb -s shell") 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).我试图访问外部存储中的文件而不是需要 root 访问的内部文件(尽管我不知道为什么我可以在命令提示符 window 中访问完全相同的文件而不允许 root 访问)。 If you want to access external files, add root access with如果要访问外部文件,请添加 root 访问权限

adb root

Full code that works for anyone who might face the same issue:适用于可能面临相同问题的任何人的完整代码:

public void FilePush() {
        try{

        String androidFilePath = "/storage/emulated/0/Android/data/com.example.myapp/files/myfile.txt";

        String windowsFilePath = "C:\\Users\\myUsername\\Downloads\\Sources";

        List<String> cmd = new LinkedList<>();

        cmd.add("C:\\Users\\myUsername\\AppData\\Local\\Android\\Sdk\\platform-tools\\adb.exe");
        cmd.add("pull");
        cmd.add(androidFilePath);
        cmd.add(windowsFilePath);

        ProcessBuilder pb = new ProcessBuilder(cmd);
        pb.redirectErrorStream(true);
      

        String line = "null";



        pb.redirectErrorStream(true); // can use these 2 line if you want to see output or errors in file.
         pb.redirectOutput(new File("C:\\Users\\myUsername\\Downloads\\Sources\\logs.txt"));

        Process p = pb.start();

        while(p == null)
            Thread.sleep(1000);

        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));

        Pattern pattern = Pattern.compile("^([a-zA-Z0-9\\-]+)(\\s+)(device)");
        Matcher matcher;


        while ((line = in.readLine()) != null) {
            if (line.matches(pattern.pattern())) {
                matcher = pattern.matcher(line);
                if (matcher.find()) ;
            }

        }

    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM