SladeX Zengimana
SladeX Zengimana

Reputation: 33

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. 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

Upvotes: 0

Views: 708

Answers (2)

SladeX Zengimana
SladeX Zengimana

Reputation: 33

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

Upvotes: 0

Diego Torres Milano
Diego Torres Milano

Reputation: 69188

Remove the line

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

as you don't want shell but pull.

Upvotes: 1

Related Questions