public static void main(String[] Args) {
    // System.setProperty("KEY", "HNY81514525MALVIYA");
    // Map<String, String> env = System.getenv();
    // for (String envName : env.keySet())
    // System.out.format("%s = %s%n", envName, env.get(envName));

    // reg add HKCU\\Environment /v developer /d \"Honey Keny Malviya" /f"

    try {
      String PERSONAL_FOLDER_CMD = "reg query HKCU\\Environment /v developer";
      String REGSTR_TOKEN = "REG_SZ";
      Process process = Runtime.getRuntime().exec(PERSONAL_FOLDER_CMD);
      StreamReader reader = new StreamReader(process.getInputStream());
      reader.start();
      process.waitFor();
      reader.join();
      String result = reader.getResult();
      int p = result.indexOf(REGSTR_TOKEN);
      if (p == -1) return;
      System.out.println(result.substring(p + REGSTR_TOKEN.length()).trim());
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }
  }
Esempio n. 2
0
  private String readRegistry(String location, String key) {
    String result = null;
    Process process = null;
    try {
      process = Runtime.getRuntime().exec("reg query " + '"' + location + "\" /v " + key);

      StreamReader reader = new StreamReader(process.getInputStream());
      reader.start();
      process.waitFor();
      reader.join();

      String[] parsed = reader.getResult().split("\\s+");

      if (parsed != null) {
        for (int i = 0; i < parsed.length; i++) {
          if (parsed[i].toLowerCase().indexOf("%systemdrive%") != -1
              || parsed[i]
                      .toLowerCase()
                      .indexOf(System.getenv("SystemDrive").toLowerCase() + File.separator)
                  != -1) {
            result = parsed[i];
          } else {
            if (result != null) {
              result += " " + parsed[i];
            }
          }

          // if (parsed[i].toLowerCase().indexOf("%systemdrive%") ==
          // -1) {
          // if (result != null) {
          // result += " " + parsed[i];
          // }
          // } else {
          // result = parsed[i];
          // }
        }
      }

      if (result == null) {
        result = System.getenv("SystemDrive") + File.separator;
      }

      // if (parsed.length > 1) {
      // result = parsed[parsed.length - 1];
      // }
      // System.out.println(result);
      result = result.replace("%SystemDrive%", System.getenv("SystemDrive"));
    } catch (Exception e) {
      result = System.getenv("SystemDrive") + File.separator;
    }

    return result;
  }
 /** read inputstream before process.waitfor(). keep threads never hangs up. */
 @Override
 public void run() {
   try {
     StreamReader reader = new StreamReader(process.getInputStream());
     reader.start();
     reader.join(readTimeOut);
     process.waitFor();
   } catch (InterruptedException ignore) {
     return;
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
Esempio n. 4
0
  /**
   * Executes a command as {@code user}. Saves the stdout/stderr in the corresponding fields and
   * returns the return code of the child process.
   *
   * @param cmd The command to execute
   * @param data The data to send to the stdin of the process
   * @param timelimt How many seconds the command can run
   * @param memlimit How many megabytes the command can use
   */
  private int exec(String cmdS, String data, int timelimit, int memlimit) throws Exception {
    ArrayList<String> cmd = new ArrayList<String>(cmdPrefix);
    cmd.add("-m");
    cmd.add("" + memlimit);
    cmd.add("-c");
    cmd.add("" + timelimit);
    cmd.add("-w");
    cmd.add("" + (3 * timelimit + 1));
    cmd.add("-x");
    cmd.add(cmdS);
    String tmp = "";
    for (String cs : cmd) tmp += " \"" + cs + "\"";
    log.fine("exec " + tmp);
    ProcessBuilder pb = new ProcessBuilder(cmd);
    pb.directory(workDir);
    Process p = pb.start();
    OutputStreamWriter writer = new OutputStreamWriter(p.getOutputStream());
    StreamReader rOut = new StreamReader(p.getInputStream());
    StreamReader rErr = new StreamReader(p.getErrorStream());
    rOut.start();
    rErr.start();

    // TODO I think this may block for big tests. Check and Fix.
    // send and receive data "in parallel"
    writer.write(data);
    writer.flush();
    writer.close();
    rOut.join();
    rErr.join();
    stdout = rOut.result;
    stderr = rErr.result;
    if (rOut.exception != null) throw rOut.exception;
    if (rErr.exception != null) throw rErr.exception;

    // log.finer("out: " + stdout);
    // log.finer("err: " + stderr);
    // log.finer("done exec");
    return p.waitFor();
  }