简体   繁体   中英

Getting exception while running adb commands programmatically

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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