title: Is it possible to run cmd through a java program
body: <p><strong>I would like to know if its possible to run cmd through Java</strong>.  Not just one command at a time but a <strong>continuous stream of user input commands that relays the info received</strong>.  Is this possible or should I just stop trying now?</p>
  
  <p>I'm not sure why I'm attaching this; it's not very relevant, but this is what I was trying to accomplish this with.  However, it resets the <code>cmd</code> after every command.  (Yes, I realize this is bad coding, but I'm just attempting something my boss asked about.)</p>
  
  <pre><code>import java.io.*;
  import java.util.Scanner;
  
  public class Cmd {
  
      public static void main(String[] args) throws IOException {
  
          String line;
  
          while (true) {
              Scanner scanner = new Scanner(System.in);
              String comm = scanner.nextLine();
              ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", comm);
              builder.redirectErrorStream(true);
              Process p = builder.start();
              BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
  
              while (true) {
              line = r.readLine();
              if(line==null){break;}
                  System.out.println(line);
             }
         }
     }
  }
  </code></pre>
  
  <p>Basically, <code>cmd</code> but behind a Java GUI with user input commands is my end game.  If anyone could tell me if this is possible and if so point me in the right direction I would be grateful.</p>