0

I want to execute "adb" command using Java. I tried out as follow:

Process p = Runtime.getRuntime().exec(new String[]{"cmd","/c","adb devices"});

But, I get following error p.getErrorStream():

'adb' is not recognized as an internal or external command,operable program or batch file.

Is there any problem of space between "adb devices"?

How to add space in command?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Sachin J
  • 2,081
  • 12
  • 36
  • 50

2 Answers2

3

The problem is not the space, but the fact that adb is not found (because it's not on the path).

Do one of those two things:

  • make sure that the command is executed at the place where adb.exe resides or
  • modify PATH (for that command) in a way that adb.exe is in a directory mentioned in PATH.

While both of those are possible with Runtime.exec(), I'd suggest using ProcessBuilder, because it has a much nicer/easier API.

For example to modify the path where the command is executed do this:

ProcessBuilder pb = new ProcessBuilder("cmd","/c","adb devices");
pb.directory(new File("c:\\path\\to\\android\\platform-tools\\");
pb.start();
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
0

Most probably problem is in your %PATH% ( or lack thereof ) - it seems that it separated arguments properly. YO may try to use absolute path to adb executable

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35