开发者

Java: Reading Windows Registry keys stopped working

I have been reading keys and values by executing "reg query", but a few days ago it stopped working. I haven't changed any code. Reading a key value still works fine:

        Process process = Runtime.getRuntime().exec(
                "reg query " + "\"" + root + key + "\" /v " + valueName);
        InputStreamReader inputreader = new InputStreamReader(
                process.getInputStream());
        BufferedReader reader = new BufferedReader(inputreader);
        process.waitFor();
        String line = null;
        while ((line = reader.readLine()) != null) {
            if (line.contains("REG_SZ")) {
                Pattern pattern = Pattern.compile("REG_SZ\\s*(.*)");
                Matcher matcher = pattern.matcher(line);
                while (matcher.find()) {
                    return line.substring(matcher.start(1));
                }
            } else if (line.contains("REG_DWORD")) {
                Pattern pattern = Pattern.compile("REG_DWORD\\s*(.*)");
                Matcher matcher = pattern.matcher(line);
                while (matcher.find()) {
                    return line.substring(matcher.start(1));
                }
            }
        }

But the process.waitFor() when reading keys never returns. This worked until a few days ago:

        Process process = Runtime.getRuntime().exec(
                "reg query " + "\"" + root + key + "\"");
        InputStreamReader inputreader = new InputStreamReader(
                process.getInputStream());
        BufferedReader reader = new BufferedReader(inputreader);
        process.waitFor();
        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(">>"+line);
            if (line.contains(root)) {
                list.add(line.substring(line.lastIndexOf('\\') + 1));
            }
        }
        String[] keys = new String[list.size()];
        return list.toArray(keys);

If I run the query usin开发者_JAVA技巧g the windows command line it works fine, so it's not my query that's wrong... I have the applet running on a server, but two days ago our external testers reported, that the applet was freezing. It was however still working for me and my colleagues. This morning it froze for me too and it haven't worked since...


Often, a waitFor() call fails to return because the child process is blocked on a write.

Since you aren't consuming the output of the reg command while it runs, it's possible that it has filled the output buffer, and it is waiting for you to read some of the output before it writes more.

For example, if more values were added to a registry key, this could break a query that worked previously.

I recommend creating one thread for reading the standard output stream of the process, and another to read its standard error stream. The standard output stream will signal EOF when you've read all of the output (this will cause your BufferedReader to return null).

At this point, a call to waitFor() should not block (this not a safe assumption in general, but it's okay for the reg command). Finally, you should destroy() the process.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜