1

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?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132

2 Answers2

3

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");    
Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • As I understand, when i use "/" i'm always linking from root. But, it doesn't matter. Using next code you'll get same result. Preferences p = Preferences.userRoot(); p.nodeExists(.../Microsoft/Windows"); - false p.nodeExists(.../Microsoft")); - true – Stan Kurilin Mar 30 '10 at 20:14
  • @Stas You are right (according to the API docs). Puzzling... I actually tried to reproduce it on my machine but I couldn't even get as far as you: could only get a totally empty root node :-( – Péter Török Mar 30 '10 at 21:06
  • 1
    @Stas I guess it might be somehow related to privileges... What OS and JDK you are using? – Péter Török Mar 30 '10 at 21:22
  • I'm using Win Xp Pro Sp3 with jdk1.6.0_10. User - admin. – Stan Kurilin Mar 31 '10 at 18:38
  • 1
    @Stas That explains why you see those nodes and I don't... Unfortunately I can't log in as admin on my work machine :-( – Péter Török Apr 01 '10 at 12:54
  • @Péter Török Thanks for help. I'm solved my promlem in with out it. But, in future, i'll return to this question. – Stan Kurilin Apr 02 '10 at 19:56
1

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.

peterh
  • 18,404
  • 12
  • 87
  • 115