Add space in adb command

丶灬走出姿态 提交于 2020-01-06 06:34:05

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/12814103/add-space-in-adb-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!