title: Get Command Prompt Output to String In Java
body: <p>I need a java method that will read command prompt output and store it into a String to be read into Java.</p>
  
  <p>This is what I have so far but isn't working right.</p>
  
  <pre><code>public void testGetOutput() {
      System.out.println("\n\n****This is the testGetOutput Method!****");
      String s = null;
      String query = "dir " + this.desktop;
      try {
          Runtime runtime = Runtime.getRuntime();
          InputStream input = runtime.exec("cmd /c " + query).getInputStream();
          BufferedInputStream buffer = new BufferedInputStream(input);
          BufferedReader commandResult = new BufferedReader(new InputStreamReader(buffer));
          String line = "";
          try {
              while ((line = commandResult.readLine()) != null) {
                  s += line + "\n";
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          System.out.println(s);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }//end testGetOutput()
  </code></pre>
  
  <p>I think the problem is when I try to change the query to be a command which will execute HandBrakeCLI.exe. Looking at my system when the program is running (but seems to have paused), it shows me that HandBrakeCLI.exe is running under a cmd window which is being run under my IDE. All that makes sense, but the HandBrakeCLI.exe doesn't exit, so I'm guessing that's why I can't read the output as input to my program.</p>
  
  <p>So, after that background. My big question is this: How do I get HandBrakeCLI.exe to close after it's finished with my query so I can get its output?
  Just for extra info, the only difference between the method above and the scan DVD method I have for HandBrakeCLI is the query variable is different. Like this example:</p>
  
  <pre><code>String query = "C:\Users\Kent\Desktop\HBCLI\HandBrakeCLI -t --scan -i "C:\Users\Kent\Desktop\General Conference DVDs\Sources\174th October 2004\DVD 1"; //this is actually a variable in the DVD object, but here's an example'
  </code></pre>
  
  <p>Oh, and by the way, when I run that query in a regular command prompt, it does exactly what I want it to, giving me all the output I desperately desire!</p>
  
  <p>Here's the original problem (I'm not sure how to resubmit a question):</p>
  
  <p>I've been looking everywhere and can't figure this out. I'm not sure what I've found is even relevant to what I want to do. I don't have a whole lot of code for it yet, so it wont do much to put code here and I think this should be pretty simple, so I'm going to give some screenshots here. So here's my task:</p>
  
  <ol>
  <li><p>Scan folder which is full of ripped DVD folders (Video_TS folders with VOB files etc.) and store these folder names as the title of the DVD.</p></li>
  <li><p>Scan each folder using the HandBrakeCLI and store the output to a string.</p></li>
  <li><p>Regex the string to identify each title, chapter, and language.</p></li>
  <li><p>Generate queries to give back to HandBrakeCLI to bulk encode each language in each chapter in each title for each DVD (you can see why I want to automate this!)</p></li>
  <li><p>Store these queries in a *.bat file</p></li>
  </ol>
  
  <p>The only part I'm not sure about is step 2! I can do everything else pretty easily. I've read a lot about OutputStreams, but I just can't seem to understand how it works. I really just need to get the output to a string which I can regex to get the stuff I need. Here are the screenshots of what I need to input and what I need to strip from the output:</p>
  
  <p>Input to HandBrakeCLI:</p>
  
  <p><img src="https://i.stack.imgur.com/CAY5Y.jpg" alt="enter image description here"></p>
  
  <p>Output to scan:</p>
  
  <p><img src="https://i.stack.imgur.com/wfehl.jpg" alt="enter image description here"></p>