2

I have written the following code and cannot quite figure out how to solve the error. Not sure if this information will be found useful, but I am using a Mac and using the editor IntelliJ.

public class TestCode {
    public static void main(String[] args) throws Exception {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("adb devices");
    }
}

The result is "Exception in thread "main" java.io.IOException: Cannot run program "adb": error=2, No such file or directory"

However, when I run the command "adb devicees" from the terminal I get the list of devices attached to my computer.

For those interested, the following is the full stack trace.

Exception in thread "main" java.io.IOException: Cannot run program "adb": error=2, No such file or directory
    at java.lang.ProcessBuilder.processException(ProcessBuilder.java:478)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:457)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:431)
    at java.lang.Runtime.exec(Runtime.java:328)
    at com.sonos.acr.test.TestCode.main(TestCode.java:6)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:53)
    at java.lang.ProcessImpl.start(ProcessImpl.java:91)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
    ... 9 more

Thank you in advance for suggestions, advice, and/or help.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
papezjustin
  • 2,223
  • 6
  • 24
  • 31

1 Answers1

5

You need to use the exec(String[] cmdarray) function to send arguments, the single argument version of the function splits the string on space, and that spells trouble if your path contains spaces.

you also need to specify the full path (maybe /usr/bin/adb?).

Like this:

Process process = runtime.exec(new String[] {"/usr/bin/adb", "devices"});
Alexander Kjäll
  • 4,246
  • 3
  • 33
  • 57
  • I have tried making the changes you suggested, and also added the full path to adb, which on my machine is: /Applications/Android\ SDK/platform-tools/, unfortunately, the same error still persists. – papezjustin Jul 18 '13 at 13:07
  • 1
    @JustinPapez Does the "Android SDK" contains a space in its name or is it just a typo in your comment? Try to copy the command to a directoy of your choice to a path with no spaces to check if the same error persists – c.s. Jul 18 '13 at 13:15
  • I tested this on my linux box, and the following works fine for me: Runtime.getRuntime().exec(new String[]{"/tmp/space dir/ls", "-l"}); Note that I don't escape the space in the directory name. – Alexander Kjäll Jul 18 '13 at 13:40
  • Both escaping and not escaping the space resulted in an error for me; however, user "c.s." led me in the right direction by inquiring about a typo. There was no typo but renaming the "Android SDK" folder to "Android-SDK" affectively solved the problem by eliminating whitespace. Thank you everyone for your help. – papezjustin Jul 18 '13 at 14:01