问题
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 thatadb.exe
is in a directory mentioned inPATH
.
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