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

走远了吗. 提交于 2019-12-06 12:46:09

问题


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/Policies/Microsoft/Windows") 

return false?


回答1:


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");    



回答2:


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.



来源:https://stackoverflow.com/questions/2548307/working-with-windows-registry-using-java-util-prefs-preferences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!