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.