OK, no other answers, so I'll have a go.
This is coding on wild side! You use refection to break into private methods that are not part of the public API. Even if you get this to work now, there's no guarantee that it won't break in the very next minor Java update. Seems dangerous to me.
Anyway, try it without the HKEY_LOCAL_MACHINE\\ in your key, I think that's already implied by systemRoot.
If you want a safer way to approach this, you can use ProcessBuilder to run the command line registry tool reg.exe, and stick to published public APIs. I can let you have some sample code if you want to try it.
JamesCherrill
Posting Genius
6,337 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,070
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Here's a small program that will query a value from the registry. It runs reg.exe with the appropriate command then parses the out pt to get the result. If you look at the help messages for reg.exe you'll see the other options, and you'll then need to add some parsing to get whatever other results you need. The only other options I've ever found are (1) a neat Java/dll package called LatteLib that works well, but seems to have vanished from the net a couple of years ago, and (2) very many variants on the code that hooks into the private methods of the Java Windows Preferences implementation (with the dangers that I mentionsed in my earlier post).
Anyway, here's some code that may be useful to you:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class WindowsRegistry {
// just read a simple value so far...
// see reg.exe help (in a command window) for other options
public static String queryValue(String key, String value) {
// returns null if key/value not found (or other error)
try {
Process p = new ProcessBuilder("reg.exe", "QUERY", key, "/v", value)
.redirectErrorStream(true).start();
// (reg.exe requires parameters to be already tokenised)
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
// scan output & parse interesting lines...
while ((line = br.readLine()) != null) {
System.out.println("Input: " + line);
if (line.contains(value)) {
// this line displays the value we are looking for
int i = line.indexOf("REG_SZ");
if (i >= 0) {
String result = line.substring(i + 7).trim();
is.close();
return result;
}
}
if (line.contains("Error")) {
System.err.println("reg.exe error searching for: " + key
+ " " + value);
System.err.println(line);
is.close();
return null;
}
}
System.err.println("ERROR: unable to parse output from reg.exe");
System.err.println("Searching for: " + key + " " + value);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String args[]) throws IOException {
// a little demo...
String res;
res = WindowsRegistry.queryValue("HKCU\\Control Panel\\Desktop",
"Wallpaper");
System.out.println("Wallpaper path: \"" + res + "\"");
res = WindowsRegistry.queryValue("HKCU\\Control Panel\\Desktop",
"noSuchValue");
System.out.println("Incorrect query returned: \"" + res + "\"");
}
}
JamesCherrill
Posting Genius
6,337 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,070