在Java程序中使用ADB pull命令

发布于 2025-01-14 04:33:10 字数 1239 浏览 2 评论 0原文

我无法让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山田美奈子 2025-01-21 04:33:10

删除该行

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

,因为您不需要 shell,而是 pull

Remove the line

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

as you don't want shell but pull.

书信已泛黄 2025-01-21 04:33:10

感谢这里乐于助人的人们,我终于成功了!我首先需要将 adb.exe 作为命令运行,然后才能运行任何 adb 命令。我需要从命令列表中删除 ("adb -s shell") 。我试图访问外部存储中的文件而不是需要根访问权限的内部文件
(尽管我不知道为什么我可以在命令提示符窗口中访问完全相同的文件而不允许根访问)。如果您想访问外部文件,请使用

adb root

添加 root 访问权限适用于可能面临相同问题的任何人的完整代码:

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();
    }
}

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 :

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文