1

I am trying to get list of adb devices through code but getting an exception when starting the ProcessBuilder.

Here is my Code -->

try {
                ProcessBuilder pb = new ProcessBuilder("adb.exe", "adb devices");
                pb.directory(new File("C:\\Users\\user\\AppData\\Local\\Android\\sdk\\platform-tools"));

                Process p = pb.start(); // here is the xception
                BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));  
                String line = null;  

                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())
                            System.out.println(matcher.group(1));
                    }
                } 

Here is the Exception stacktrace -->

java.io.IOException: Cannot run program "adb.exe" (in directory "C:\Users\user\AppData\Local\Android\sdk\platform-tools"): CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(Unknown Source) at MainTest.Example.main(Example.java:45) Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(Unknown Source) at java.lang.ProcessImpl.start(Unknown Source) ... 2 more

I have tried after changing commands in ProcessBuilder but no luck.

Need help please.

3 Answers 3

3

this should work

ProcessBuilder pb = new ProcessBuilder("C:\\Users\\<your user name>\\AppData\\Local\\Android\\sdk\\platform-tools\\adb.exe", "adb devices");

Do not forget: <your user name> - set your name

or put C:\Users\<your user name>\AppData\Local\Android\sdk\platform-tools\ into windows PATH variable.

Sign up to request clarification or add additional context in comments.

7 Comments

Now I can see adb.exe is started in Task Manager but it seems adb devices command is not executed and flow is stuck at while ((line = in.readLine()) != null).
@vv88 , first of all , change new ProcessBuilder("adb.exe", "adb devices"); into ` new ProcessBuilder("adb.exe", "adb","devices");`
I have change it to ProcessBuilder pb = new ProcessBuilder("C:\\Users\\vv88\\AppData\\Local\\Android\\sdk\\platform-tools\\adb.exe", "adb", "devices"); but still stuck at same point. Thanks for helping me out.
@vv88 does simple pb.waitFor() works? or it hands too?
I have put p.waitFor() after pb.start(). As per javadoc waitFor() method will wait untill process represented by p is not terminated. So now I am stuck at `p.waitFor()'.
|
2

After a lot of help and suggestions from Vyacheslav over the chat here is the working solution for the community -->

try 
            {

                ProcessBuilder pb = new ProcessBuilder("C:\\Users\\<Your User Name Here>\\AppData\\Local\\Android\\sdk\\platform-tools\\adb.exe", "devices");

                //pb.redirectErrorStream(true); // can use these 2 line if you want to see output or errors in file.
                //pb.redirectOutput(new File("C:/pbOutput.Txt"));

                Process p = pb.start();

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

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

                String line = null;  

                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())
                            System.out.println(matcher.group(1));
                    }
                }  
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }

I just need to find out why "adb devices" did not work.

Comments

0

This will for mac If yor are using zsh :

Runtime.getRuntime().exec(new String[]{"zsh", "-l", "-c", commands})

in commands we can pass "adb devices" or another commands

At my end its working

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.