FAQ

Java

JSP

Servlet


Advertisement



How to Access Windows Registry in Java?

The java.util.prefs.Preferences class allows us to access information from the Windows registry. This class allows applications to store and retrieve user and system preference and configuration data.

There are two separate trees of preference nodes, one for user preferences and one for system preferences. The java.util.prefs.Preferences abstract class provides two static methods: userRoot and systemRoot that return references to the root node of the user preference tree and the system preference tree respectively. Each user has a separate user preference tree, and all users in a given system share the same system preference tree.

Nodes in a preference tree are named in a similar fashion to directories in a hierarchical file system. Every node in a preference tree has a node name (which is not necessarily unique), a unique absolute path name, and a path name relative to each ancestor including itself.

In Microsoft window Systems, HKEY_CURRENT_USER\JavaSoft\Prefs is for user Preferences and HKEY_LOCAL_MACHINE\JavaSoft\Prefs is for system Preferences. For example,

public class WindowRegistry {

public static final String REG_KEY = "WebSite";

public static void main(String args[]) {

//Retrieve a reference to the root of the user preferences tree
final Preferences userRoot = Preferences.userRoot();

// Write Preferences information to HKCU (HKEY_CURRENT_USER),
// HKCU\Software\JavaSoft\Prefs\WebSite
userRoot.put(REG_KEY, "www.xyzws.com");

//Retrieve a reference to the root of the system preferences tree
final Preferences systemRoot = Preferences.systemRoot();

// Write Preferences information to HKLM (HKEY_LOCAL_MACHINE),
// HKLM\Software\JavaSoft\Prefs\WebSite
systemRoot.put(REG_KEY, "www.xyzws.com");

// Read back the value we've written in the code above.
//
System.out.println(REG_KEY + " = "
+ systemRoot.get(REG_KEY, REG_KEY + " was not found."));
}
}

There are two other static methods in the Preferences class that return a Preferences instance, Preferences.userNodeForPackage(Class c) and Preferences.systemNodeForPackage(Class c), which is associated (by convention) with the specified class's package.

public class WindowRegistry {
public static void main(String args[]) {

//Retrieve a reference to the root of the user preferences tree
final Preferences userRoot = Preferences.userRoot();

//Returns the preference node from the calling user's preference tree
//that is associated (by convention) with the specified class's package.
//The convention is as follows: the absolute path name of the node is the
//fully qualified package name, preceded by a slash ('/'), and with each
//period ('.') replaced by a slash.
final Preferences usr = Preferences.userNodeForPackage(
userRoot.getClass());

// Write Preferences information to HKCU (HKEY_CURRENT_USER),
// HKCU\Software\JavaSoft\Prefs\java\util\prefs
usr.putFloat("f-value", 1.2f);

//Retrieve a reference to the root of the system preferences tree
final Preferences systemRoot = Preferences.systemRoot();

//Returns the preference node from the system preference tree that is
//associated (by convention) with the specified class's package.
//The convention is as follows: the absolute path name of the node is
//the fully qualified package name, preceded by a slash ('/'), and
//with each period ('.') replaced by a slash.
final Preferences sys = Preferences.systemNodeForPackage(
systemRoot.getClass());

//Write Preferences information to HKLM (HKEY_LOCAL_MACHINE),
//HKLM\Software\JavaSoft\Prefs\java\util\prefs because
//systemRoot's fully qualified
//package name is java.util.prefs.
sys.putFloat("f-value", 1.2f);

}
}

The default java.util.prefs.WindowsPreferences class is Windows registry based implementation of Preferences. It is Java wrapper for Windows registry APIs but it is default "class" and you can not access it from outside of java.util.prefs package. You need to use Java reflection to access these methods. Here is source code for java.util.prefs.WindowsPreferences class. Here is an example based on this blog:

public class WindowRegistry {

private static final int KEY_READ = 0x20019;

public static void main(String args[]) {
currentUser();
currentMachine();
}

private static void currentMachine() {

//Retrieve a reference to the root of the system preferences tree
final Preferences systemRoot = Preferences.systemRoot();
final Class clz = systemRoot.getClass();

try {
Class[] params1 = {byte[].class, int.class, int.class};
final Method openKey = clz.getDeclaredMethod(
"openKey", params1);
openKey.setAccessible(true);

Class[] params2 = {int.class};
final Method closeKey = clz.getDeclaredMethod(
"closeKey", params2);
closeKey.setAccessible(true);

final Method winRegQueryValue = clz.getDeclaredMethod(
"WindowsRegQueryValueEx",
int.class,
byte[].class);
winRegQueryValue.setAccessible(true);

String key = "SOFTWARE\\Microsoft\\Internet Explorer";
int hKey = (Integer) openKey.invoke(systemRoot,
toByteEncodedString(key),
KEY_READ,
KEY_READ);
byte[] valb = (byte[]) winRegQueryValue.invoke(systemRoot, hKey,
 toByteEncodedString("Version"));
String vals = (valb != null ? new String(valb).trim() : null);
System.out.println("Internet Explorer Version = " + vals);
closeKey.invoke(Preferences.systemRoot(), hKey);

} catch (Exception e) {
e.printStackTrace();
}
}

private static void currentUser() {
//Retrieve a reference to the root of the user preferences tree
final Preferences userRoot = Preferences.userRoot();
final Class clz = userRoot.getClass();

try {
Class[] params1 = {byte[].class, int.class, int.class};
final Method openKey = clz.getDeclaredMethod("openKey",
params1);
openKey.setAccessible(true);

Class[] params2 = {int.class};
final Method closeKey = clz.getDeclaredMethod("closeKey",
params2);
closeKey.setAccessible(true);


final Method winRegQueryValue = clz.getDeclaredMethod(
"WindowsRegQueryValueEx",
int.class,
byte[].class);
winRegQueryValue.setAccessible(true);

String key = "Software\\Microsoft\\Windows\\" +
"CurrentVersion\\Internet Settings";
int hKey = (Integer) openKey.invoke(userRoot,
toByteEncodedString(key),
KEY_READ,
KEY_READ);

byte[] valb = (byte[]) winRegQueryValue.invoke(
userRoot,
hKey,
toByteEncodedString(
"MimeExclusionListForCache"));

String vals = (valb != null ? new String(valb).trim() : null);
System.out.println("MimeExclusionListForCache = " + vals);
closeKey.invoke(Preferences.userRoot(), hKey);

} catch (Exception e) {
e.printStackTrace();
}
}

private static byte[] toByteEncodedString(String str) {

byte[] result = new byte[str.length() + 1];
for (int i = 0; i < str.length(); i++) {
result[i] = (byte) str.charAt(i);
}
result[str.length()] = 0;
return result;
}
}


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |