开发者

Working with Windows registry using java.util.prefs.Preferences

I have some questions about the registry.

We have

Preferences p = Preferences.userRoot();

If we execute

p.nodeExists("/HKEY_CURRENT_USER/Software/Policies/Microsoft")    

it will return true.

After it:

p = p.node("/HKEY_CURRENT_USER/Software/Policies");    
for(String s : p.childrenNames()){
    System.out.println(">" + s);
}

We see that it has one child: "Windows". But

p.nodeExists("/HKEY_CURRENT_USER/Software/Policies/Microsoft/Windows")

returns false. Why?

Thanks.

UPDATE

Ok. I have some mistakes. Let me try again: Why does

p.nodeExists("/HKEY_CURRENT_USER/Software/Pol开发者_开发知识库icies/Microsoft/Windows") 

return false?


If you execute the code lines shown, in the order shown, when you get to the line

p.nodeExists("/HKEY_CURRENT_USER/Software/Policies/Microsoft/Windows")

p does not anymore point to the user root, but to "/HKEY_CURRENT_USER/Software/Policies".

Btw you have a probable omission in your third code sample:

p = p.node("/HKEY_CURRENT_USER/Software/Policies");    

should be

p = p.node("/HKEY_CURRENT_USER/Software/Policies/Microsoft");    


I stumbled on this one today. The answer you've accepted is completely wrong.

You seem to be under the impression that Java Preferences is a general tool to manipulate the Windows Registry. It is not. It just so happens that the default implementation of Preferences on the Windows platform happens to store its data in the Windows Registry.

The implementation on Windows stores stuff under the following Registry paths:

For systemRoot: HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Prefs

For userRoot : HKEY_CURRENT_USER\Software\JavaSoft\Prefs

(note: The registry paths changes a bit if you are using a 32bit JRE on a 64-bit OS but that has nothing to do with Java and everything to do with Windows. Sun's code always use the above paths.)

The point to make is that you can maybe use Java Preferences interface to read or change values in the Windows Registry but only below the above registry paths. The reason why I say 'maybe' is that this is just how it happens to be at the moment. Sun/Oracle could at any point in time decide to not to use the Windows Registry or use the Windows Registry but without using sub-nodes, i.e. store everything in one big XML string or something. The point is that Java Preferences is designed to shield you from this.

A lot of Java software that use the Java Preferences provide their own implementation (which is pretty simple to do) to avoid Sun's default implementation that uses the Windows Registry. Not everyone can write to the Windows Registry these days so that was a pretty bad design decision on Sun's part. Fortunately very easy to change.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜