title: Sending commands to a console application?
body: <p>I want to verify something, because in my head it makes sense, but in Java, it doesn't work.</p>
  
  <p>I am trying to run another Jar file through my application. A Minecraft server, to be precise. I have all the basics down (using <code>ProcessBuilder</code>, executing with arguments, waiting for an exit code, etc.), but there is one thing that I cannot figure out. Sending commands to the application. Here part of my <code>CommandLineSender</code> class:</p>
  
  <pre><code>public class CommandLineSender extends Thread {
  
      private BufferedWriter output;
      private InputStream source;  // Set to System.in when creating the object
      private boolean stopRequested;
  
      public CommandLineSender(Process sendTo, InputStream source) {
          this.output = new BufferedWriter(new OutputStreamWriter(sendTo.getOutputStream()));
          this.source = source;
          System.out.println("Source InputStream initiated: " + source.toString());
          this.stopRequested = false;
      }
  
      @Override
      public void run() {
          System.out.println("Run called.");
          Scanner cmdScanner = new Scanner(source);
          while (cmdScanner.hasNextLine() &amp;&amp; !stopRequested) {
              System.out.println("Has next line");
              String msg = cmdScanner.nextLine();
              write(msg);
              System.out.println("Wrote: " + msg);
          }
  
          // Close the scanner and BufferedWriter
  
          System.out.println("Closed.");
  
      }
  
      // Other various methods
  
      protected void write(String msg) {
          try {
              output.write(msg);
          } catch (IOException e) {
              System.err.println("Unable to write message because of an unhandled IOException: " + e.getMessage());
          }
  
      }
  </code></pre>
  
  <p>The output I get is this:</p>
  
  <pre><code>(Default Minecraft server output)
  
  help  // My command
  Has next line
  Wrote: help
  </code></pre>
  
  <p>This may not matter, but I am executing my server with these arguments:</p>
  
  <pre><code>java -Xmx1024M -Xms1024M -jar (path to server jar) nogui
  </code></pre>
  
  <p>Thank you for your time.</p>