code: public static Process runProcess(String[] cmdArray, File logFile, File dir) {
          try {
      
              ProcessBuilder pb = new ProcessBuilder(cmdArray);
              pb.redirectErrorStream(true);
              pb.directory(dir);
      
              Process proc = pb.start();
              if (logFile != null) {
                  // Redirect stderr and stdout to logFile
                  // TODO: in Java 7, use redirectOutput(Redirect.to(logFile))
                  InputStream inputStream = new BufferedInputStream(proc.getInputStream());
                  BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(logFile));
      
                  int read = 0;
                  byte[] bytes = new byte[1024];
      
                  while ((read = inputStream.read(bytes)) != -1) {
                      out.write(bytes, 0, read);
                  }
      
                  inputStream.close();
                  out.flush();
                  out.close();
              }
              proc.waitFor();
              return proc;
      
          } catch (Exception e) {
              String tail = "";
              try {
                  tail = QFiles.tail(logFile);
              } catch (Throwable t) {
                  // Ignore new exceptions
              }
              throw new RuntimeException("Exception thrown while trying to exec command " + System.cmdToString(cmdArray) + "\n"
                      + tail, e);
          }
      }
docs: Returns the Process which contains the exit code.