code: public int exec(String cmd, Map<String, String> env, OutputStream stdout, OutputStream stderr, ByteArrayOutputStream
            output, File dir)
            throws IOException, InterruptedException {
          //logger.debug("Executing '" + cmd + "'");
          if (output == null) {
              output = new ByteArrayOutputStream();
          }
          try {
              String[] envp;
              if (env != null) {
                  envp = new String[env.size()];
                  int i = 0;
                  for (Map.Entry<String, String> envMapEntry : env.entrySet()) {
                      envp[i++] = envMapEntry.getKey() + "=" + envMapEntry.getValue();
                  }
              } else {
                  envp = null;
              }
  
              process = Runtime.getRuntime().exec(cmd, envp, dir);
              process.getOutputStream().close();
              MyReader t1 = new MyReader(process.getInputStream(), stdout, output);
              MyReader t2 = new MyReader(process.getErrorStream(), stderr, output);
              t1.start();
              t2.start();
              int exitCode = process.waitFor();
              process = null;
              t1.cancel();
              t2.cancel();
              t1.join();
              t2.join();
              //if (output.size() > 0) {
              //    logger.debug("Executed command output:\n" + output.toString());
              //}
              return exitCode;
          } finally {
              process = null;
          }
      }
docs: Executes the a command specified in the specified directory.
  
  @param cmd a specified system command
  @param env the environment variables map to use or null to inherit process environment
  @param stdout an OutputStream for stdout
  @param stderr an OutputStream for stderr
  @param output the ByteArrayOutputStream will get a copy of the output and error streams of the subprocess
  @param dir the working directory of the subprocess, or null if the subprocess should inherit the working directory of the
  current process
  @return the cancel value of the process. By convention, 0 indicates normal termination