title: Output of forked child process in Java
body: <p>I'm executing the cmd process in Native windows from my Java program.  I want to be able to view the input and the output of the forked process.  However, I don't seem to be able to write anything to it from the Java program.</p>
  
  <p>When I try to write a set of characters onto the command prompt in windows, I'm kind of expecting it to print something to the screen - anything.  So, if I give it nonsensical garbage, then it should print that it doesn't understand that command.</p>
  
  <p>However, after writing my garbage String onto the child process's OutputStream in the following code:</p>
  
  <pre><code>Runtime runtime = Runtime.getRuntime();
  Process process = runtime.exec("cmd");
  BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
  PrintWriter out = new PrintWriter(process.getOutputStream());
  String line = null;
  out.println("My Garbage String");
  while((line = readLine()) != null)
      System.out.println(line);
  </code></pre>
  
  <p>the program stops where it says <code>while((line = readLine()) != null)</code>.</p>
  
  <p>So, the parent process is obviously blocking until the child process gets some input and unblocks and sends that input to the parent process.  But, I'm giving the child process some input when I write anything out onto the <code>PrintWriter</code>'s <code>println()</code> method, aren't I?</p>
  
  <p><em><strong>UPDATE</em></strong></p>
  
  <p>Now, because of some of the answers that were given to me, I decided to clarify what my problem is.  I can read output from the command prompt fine.  In fact, when I first run it, I get all the same ouput as you would normally get if you typed "cmd" in the command prompt.  </p>
  
  <p>For instance, take the following code example:</p>
  
  <pre><code>Process process = Runtime.getRuntime().exec("cmd");
  BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
  PrintWriter out = new PrintWriter(process.getOutputStream());
  String line = null;
  do
  {
      line = in.readLine();
      System.out.println(line);
  }while(line!=null)
  </code></pre>
  
  <p>The output of the code will be the following:</p>
  
  <blockquote>
    <p>Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001
    Microsoft Corp.</p>
  </blockquote>
  
  <p>As you can see, the command prompt is giving my parent process some output.  But, while running this in debug mode in my IDE, I find that it just simply stops at the <code>line=in.readLine();</code> line.  So, the child process blocks because there is nothing else to print.  </p>
  
  <p>So, when I try to give it some input using its <code>OutputStream</code> property, it doesn't appear to respond.  Instead, the program blocks at where <code>line=in.readLine();</code>.  </p>
  
  <p>In response to what one poster suggested, the article you mentioned (<em>When Runtime.exec() won't</em>), it only goes over getting output from the <code>OutputStream</code> and <code>ErrorStream</code> properties.  It does not mention how I can give it any kind of input other than the initial execution parameters that are given in the form of an array when you run <code>Runtime.exec(args)</code>.</p>
  
  <p>To simulate what I wanted to do initially (ie. give it random garbage and have it print that it doesn't understand the command), I <em>could</em> just simply type <code>Runtime.getRumtime().exec("cmd /c random garbage);</code> in my code and run it.  But, that isn't what I want to do.  I want to communicate with the command prompt after it's already started its execution.</p>
  
  <p>Ultimately, I want to be able to interact with other program that normally requires you to interact with them after execution from a Java program.  Yet, if the only time that I can tell it what to do is when I specify its execution parameters, then I won't be able to interact with a separate process using Java in the way that I want to.</p>
  
  <p>If anyone knows how this can be achieved, please tell me.  Thanks.</p>