title: Execute ADB command from Java program
body: <p>The program I'm working on uses ADB (Android Debug Bridge) to send files to my phone:</p>
  
  <pre><code>for (String s : files)
      String cmd = "adb -s 0123456789ABCDEF push " + s + " /mnt/sdcard/" + s;
      try {
          InputStream is = Runtime.getRuntime().exec(cmd).getInputStream();
          while (is.read() != -1) {}
      } catch (IOException e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>I want the program to wait until ADB finished the transmission, but ADB runs as a daemon and therefore never finishes. But the program continues immideately and somehow the files aren't sent to my phone (no exceptions in log). When I run the command from console, it's working without problems.</p>
  
  <p>What am I doing wrong? How do I send files via ADB correctly?</p>
  
  <p>NOTE: the <code>is.read() == -1</code> won't work, because the ADB daemon writes <em>all output</em> to the system standard output. I've tried forwarding it into a textfile. It stayed empty and the output was still written to the terminal instead</p>
  
  <p><strong>EDIT</strong>: Reading the ErrorStream of the ADB process returned the adb help for each <code>adb push</code>-command. Again: The <em>exact</em> commands (copied from Eclipse console) work in a terminal</p>
  
  <p><strong>EDIT 2</strong>: Using a ProcessBuilder instead of <code>RUntime.getRuntime.exec()</code> resulted in the following error:</p>
  
  <pre><code>java.io.IOException: Cannot run program "adb -s 0123456789ABCDEF push "inputfile "outputfile""": error=2, File or directory not found
  </code></pre>
  
  <p>at the ProcessBuilder's <code>start()</code>-method
  The same happens when using an absolute path for ADB (<code>/usr/bin/adb</code>). The inputfile and outputfile Strings are also absolute paths, like <code>/home/sebastian/testfile</code> and definitely exist. When running the commands from terminal (string "cmd" printed, copy&amp;paste), evreything still works fine.</p>