public static QTestProcessExecResult executeCmd(String[] args, String outFile, String errFile)
      throws Exception {
    System.out.println("Running: " + org.apache.commons.lang3.StringUtils.join(args, ' '));

    PrintStream out = outFile == null ? SessionState.getConsole().getChildOutStream()
      : new PrintStream(new FileOutputStream(outFile), true, "UTF-8");
    PrintStream err = errFile == null ? SessionState.getConsole().getChildErrStream()
      : new PrintStream(new FileOutputStream(errFile), true, "UTF-8");

    Process executor = Runtime.getRuntime().exec(args);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PrintStream str = new PrintStream(bos, true, "UTF-8");

    StreamPrinter errPrinter = new StreamPrinter(executor.getErrorStream(), null, err);
    StreamPrinter outPrinter = new StreamPrinter(executor.getInputStream(), null, out, str);

    outPrinter.start();
    errPrinter.start();

    int result = executor.waitFor();

    outPrinter.join();
    errPrinter.join();

    if (outFile != null) {
      out.close();
    }

    if (errFile != null) {
      err.close();
    }

    return QTestProcessExecResult.create(result,
        new String(bos.toByteArray(), StandardCharsets.UTF_8));
  }
Operations present in a given file/test.
itests/util/src/main/java/org/apache/hadoop/hive/ql/QTestResultProcessor.java