code: private static int runProcess(String[] cmd, String[] environment,
                                    File wd, InputStream in, OutputStream out,
                                    OutputStream err)
          throws IOException
      {
          Process p;
          if (environment == null) {
              p = Runtime.getRuntime().exec(cmd,null,wd);
          } else {
              p = Runtime.getRuntime().exec(cmd, environment,wd);
          }
  
          try {
              PipeThread inThread = null;
              if (in != null) {
                  inThread = new PipeThread(false, in, p.getOutputStream());
                  inThread.start();
              } else {
                  p.getOutputStream().close();
              }
  
              PipeThread outThread = null;
              if (out != null) {
                  outThread = new PipeThread(true, p.getInputStream(), out);
                  outThread.start();
              } else {
                  p.getInputStream().close();
              }
  
              PipeThread errThread = null;
              if (err != null) {
                  errThread = new PipeThread(true, p.getErrorStream(), err);
                  errThread.start();
              } else {
                  p.getErrorStream().close();
              }
  
              // wait for process completion
              for (;;) {
                  try {
                      p.waitFor();
                      if (outThread != null) {
                          outThread.join();
                      }
                      if (inThread != null) {
                          inThread.join();
                      }
                      if (errThread != null) {
                          errThread.join();
                      }
                      break;
                  } catch (InterruptedException ignore) {
                  }
              }
  
              return p.exitValue();
          } finally {
              p.destroy();
          }
      }
docs: Runs the given process using Runtime.exec().
  If any of in, out, err is null, the corresponding process stream will
  be closed immediately, otherwise it will be closed as soon as
  all data will be read from/written to process
  
  @return Exit value of process.
  @throws IOException If there was an error executing the process.