title: Java ADB shell returns nothing
body: <p>I want to write a java program to find a device and list its contents. For that I first fire "adb devices" via Java's Process class and parse the output, which works just finde. Then I want to fire this command:</p>
  
  <pre><code>adb shell -s DEVICE-SERIAL /sdcard/
  </code></pre>
  
  <p>When I put that into my terminal I get the right output, a list of all folders and files in at the root of /sdcard/.</p>
  
  <p>When I put that into Java code nothing is returned. Stderr however contains the standard help page you get when you misspell an adb command. Here's the funny thing: For debugging I output every command sent to ADB. If I run my programm and copy the exact same generated command and run it in my terminal it works just fine.</p>
  
  <p>Why is that? For reference, here are my code excerpts for that:</p>
  
  <p><strong>ADB.java:</strong></p>
  
  <pre><code>public static String runShellCommand(Device d, String command) {
      return runCommand(d, "shell " + command);
  }
  
  public static String runCommand(Device d, String command) {
      Runtime runtime = Runtime.getRuntime();
      String full = "";
      String error = "";
  
      try {
          if (d != null) {
              command = "-s " + d.getSerial() + " " + command;
          }
  
          System.out.println("Running command: adb " + command);
          Process process = runtime.exec(new String[] {ADB_LOCATION, command});
  
              process.waitFor();
  
          BufferedReader stdin = new BufferedReader(new InputStreamReader(process.getInputStream()));
          BufferedReader errin = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  
          String line;
  
          while ((line = stdin.readLine()) != null) {
              full += line;
          }
  
          while ((line = errin.readLine()) != null) {
              error += line;
          }
  
          if (!error.isEmpty()) {
              System.err.println("\n=== ADB reports: ===");
              System.err.println(error);
          }
      } catch (IOException e) {
          e.printStackTrace();
      } catch (InterruptedException e) {
          System.out.println("App was overenthuasiastic. Couldn't wait for thread to finish.");
      }
  
      return full;
  }
  </code></pre>
  
  <p><strong>Device.java:</strong></p>
  
  <pre><code>public ArrayList&lt;Folder&gt; getFoldersInDirectory(String dir) {
      String[] list = ls(dir);
  
      for (String s: list) {
          System.out.println(s);
          System.out.println(isFolderOrFile(dir + s));
      }
  
      // Just for debugging, gets completed once this runs.
      return null;
  }
  
  protected String[] ls(String dir) {
      String result = ADB.runShellCommand(this, "ls " + dir);
      System.out.println(result);
  
      return result.split("\n");
  }
  </code></pre>
  
  <p>As I said before, every other command runs just fine (I tested devices, get-state, get-serialno).</p>
  
  <p><strong>Outputs:</strong>
  <img src="https://i.stack.imgur.com/7xKE0.png" alt="Outputs"></p>