1

Possible Duplicate:
read/write to Windows Registry using Java

I want to read the registry value of the path HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS\CurrentVersion\Uninstall\{2FC099BD-AC9B-33EB-809C-D332E1B27C40}.

Can you please help me with the code?

Community
  • 1
  • 1
amod
  • 4,190
  • 10
  • 49
  • 75
  • 4
    [read/write to Windows Registry using Java](http://stackoverflow.com/questions/62289/read-write-to-windows-registry-using-java/1982033#1982033) – Prince John Wesley Jun 13 '11 at 07:07
  • hi... this code is working but can you help me in understanding this code as i am new to java. i am not getting why StreamReader is extended from Thread. – amod Jun 14 '11 at 09:48

2 Answers2

6

This shows how to read the registry, but could be extended to write operations: How to read the Windows Registry

import java.io.*;

public class RegQuery {

  private static final String REGQUERY_UTIL = "reg query ";
  private static final String REGSTR_TOKEN = "REG_SZ";
  private static final String REGDWORD_TOKEN = "REG_DWORD";

  private static final String PERSONAL_FOLDER_CMD = REGQUERY_UTIL +
    "\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\"
     + "Explorer\\Shell Folders\" /v Personal";
  private static final String CPU_SPEED_CMD = REGQUERY_UTIL +
    "\"HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\""
     + " /v ~MHz";
  private static final String CPU_NAME_CMD = REGQUERY_UTIL +
   "\"HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\""
     + " /v ProcessorNameString";

  public static String getCurrentUserPersonalFolderPath() {
    try {
      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 null;

      return result.substring(p + REGSTR_TOKEN.length()).trim();
    }
    catch (Exception e) {
      return null;
    }
  }

  public static String getCPUSpeed() {
    try {
      Process process = Runtime.getRuntime().exec(CPU_SPEED_CMD);
      StreamReader reader = new StreamReader(process.getInputStream());

      reader.start();
      process.waitFor();
      reader.join();

      String result = reader.getResult();
      int p = result.indexOf(REGDWORD_TOKEN);

      if (p == -1)
         return null;

      // CPU speed in Mhz (minus 1) in HEX notation, convert it to DEC
      String temp = result.substring(p + REGDWORD_TOKEN.length()).trim();
      return Integer.toString
          ((Integer.parseInt(temp.substring("0x".length()), 16) + 1));
    }
    catch (Exception e) {
      return null;
    }
  }

  public static String getCPUName() {
    try {
      Process process = Runtime.getRuntime().exec(CPU_NAME_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 null;

      return result.substring(p + REGSTR_TOKEN.length()).trim();
    }
    catch (Exception e) {
      return null;
    }
  }

  static class StreamReader extends Thread {
    private InputStream is;
    private StringWriter sw;

    StreamReader(InputStream is) {
      this.is = is;
      sw = new StringWriter();
    }

    public void run() {
      try {
        int c;
        while ((c = is.read()) != -1)
          sw.write(c);
        }
        catch (IOException e) { ; }
      }

    String getResult() {
      return sw.toString();
    }
  }

  public static void main(String s[]) {
    System.out.println("Personal directory : "
       + getCurrentUserPersonalFolderPath());
    System.out.println("CPU Name : " + getCPUName());
    System.out.println("CPU Speed : " + getCPUSpeed() + " Mhz");
  }
}
HackToHell
  • 2,223
  • 5
  • 29
  • 44
  • hi... this code is working but can you help me in understanding this code as i am new to java. i am not getting why StreamReader is extended from Thread – amod Jun 14 '11 at 09:55
1

There's a tutorial that shows you without using Runtime.exec() function and uses specifically java.util.prefs.WindowsPreferences.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228