title: How do you write to a command prompt in Java?
body: <p><strong>Background</strong></p>
  
  <p>I started a command prompt from a Java application. Now I want to input commands into the command prompt that was just generated. How can you run commands in the prompt that was just generated by the Java program?</p>
  
  <p><strong>What I've tried</strong></p>
  
  <p>My code creates a process that starts the command prompt. And then it gets the process's OutputStream to try and write to it. But I don't see any changes happening. It should just change directories and then run a series of commands in the new directory.</p>
  
  <pre><code>// Block that makes new command prompt
  List&lt;String&gt; commands = new ArrayList&lt;String&gt;();
  commands.add("cmd.exe");
  commands.add("/c");
  commands.add("start");
  commands.add("cmd.exe");
  
  // Block that creates a writer to write to new command prompt
  ProcessBuilder pb = new ProcessBuilder(commands);
  Process p = pb.start();
  OutputStream os = p.getOutputStream();
  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os)); 
  
  // Block that actually writes the commands
  writer.write(String.format("cd %s\n", PATH);
  writer.write(OTHER_COMMANDS); // I'm ommiting the other commands because there's a lot
  writer.flush();
  writer.close();
  </code></pre>
  
  <p>I'm unsure why the commands aren't written to the command prompt that pops up. I see that a new command prompt pops up, but the directory is unchanged from where it starts. How can I input commands into the prompt that I generate?</p>