title: Java run terminal command and send input
body: <p>I have <a href="https://masterpassword.app/" rel="nofollow noreferrer">master password for terminal</a> setup and I would like to create a Java program which adds a few extra features. To do this I need to send and receive input/output from the terminal. I tried what was suggested in <a href="https://stackoverflow.com/questions/18838904/java-program-that-runs-commands-with-linux-terminal">Java Program that runs commands with Linux Terminal</a> but didn't have any luck. For some reason the input isn't passed in and <code>Your master password:</code> is printed out if I force stop (which is were the input was supposed to be passed in). Below is my code, can anyone see what I am doing wrong?</p>
  
  <pre><code>try
  {
      // Send the command
      Process process = new ProcessBuilder("mpw", "-u", "Name", "-t", "l", "Website").start();
      String key = "somekey";
      OutputStream stdOutput = process.getOutputStream();
      // Send an input
      stdOutput.write(key.getBytes());
      // Store the input (and error) in a buffer
      BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
      BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  
      // Read the output from the command:
      int data;
      while ((data = stdInput.read()) != -1)
          System.out.write(data);
  
      while ((data = stdError.read()) != -1)
          System.out.write(data);
  
      System.out.flush();
  }
  catch (IOException e) { e.printStackTrace(); }
  </code></pre>
  
  <p>Thanks in advance</p>