title: java program hangs when ProcessBuilder executes script with input prompt
body: <p>I have a batch and shell script that execute MYSQL command.</p>
  
  <pre><code>mysql -u user -p &lt; path\etc\script\db\dosomething.sql
  </code></pre>
  
  <p>I have a java program that uses ProcessBuilder to execute the batch script.
  I am aware that this command will prompt the user for the password , I am also aware that I can alternatively use the --password (which throws an unsafe warning) or a config file to set the login parameters.
  I want to provide the password using the input prompt via ProcessBuilder but when I run the program it just hangs and I have no idea why.
  When I use JVisualVM to view what is happening I notice that the OutputStream thread is not running and if it did then what is hanging the program ? Below is my implementation:-</p>
  
  <p>Class that deals with the InputStream and ErrorStream :</p>
  
  <pre><code>public static class InStream extends Thread {
  
      private final InputStream in;
      private final PrintWriter pw;
  
      public InStream(InputStream in, PrintWriter pw, String threadName) {
          super(threadName);
          this.in = in;
          this.pw = pw;
      }
  
      @Override
      public void run() {
          try (BufferedReader br = new BufferedReader(new InputStreamReader(in));) {
              String line;
              while ((line = br.readLine()) != null) {
                  if (Thread.currentThread().isInterrupted()) {
                      Thread.currentThread().interrupt();
                      break;
                  }
                  pw.println(line);
                  pw.flush();
              }
          } catch (Exception e) {
              throw new RuntimeException(e);
          } finally {
              try {
                  in.close();
              } catch (IOException ex) {
                  throw new RuntimeException(ex);
              }
          }
      }
  }
  </code></pre>
  
  <p>Class that deals with the OutputStream:</p>
  
  <pre><code>public static class OutStream extends Thread {
  
      private final String message;
      private final OutputStream out;
  
      public OutStream(OutputStream out, String threadName, String message) {
          super(threadName);
          this.out = out;
          this.message = message;
      }
  
      @Override
      public void run() {
          try (PrintWriter pw = new PrintWriter(out);) {
              pw.println(message);
              pw.flush();
          } catch (Exception e) {
              throw new RuntimeException(e);
          } finally {
              try {
                  out.close();
              } catch (IOException e) {
                  throw new RuntimeException(e);
              }
          }
      }
  }
  </code></pre>
  
  <p>Here is the main program:</p>
  
  <pre><code>public static void main(String[] args) {
      try {
          String password = "Y0urp@ssw0rd";
          ProcessBuilder builder = new ProcessBuilder("path\\etc\\script\\cli\\batchWithSQLCommand.bat");
          Process process = builder.start();
          StringWriter errors = new StringWriter();
          StringWriter input = new StringWriter();
          new OutStream(process.getOutputStream(), "output-stream-thread", password).start();
          new InStream(process.getErrorStream(), new PrintWriter(errors, true), "error-stream-thread").start();
          new InStream(process.getInputStream(), new PrintWriter(input, true), "input-stream-thread").start();
          process.waitFor();
          System.out.println("inputStream: " + input.toString());
          System.out.println("Error Stream: " + errors.toString());
      } catch (IOException | InterruptedException ex) {
         //throw error here
      } catch (Error | Exception ex) {
           //throw error here
      }
  }
  </code></pre>
  
  <p>What could be the problem ?</p>