Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and Writing from/to Java Preferences fails on fresh Windows 8,

I have a Java application that reads from the Preferences by using:

Preferences prefs = Preferences.userNodeForPackage(MyClass.class);
prefs.get((String)key, "");

On a fresh Windows 8 machine this fails with:

WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs 
at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.

Error code 5 is access denied.

I can't find anything I'm doing wrong. Google and SO searches give old results relating only to Windows Vista/7 where one was wrongly using systemRoot (How can I write System preferences with Java? Can I invoke UAC?).

The error can be "cured" by creating HKLM/Software/JavaSoft/Prefs and setting permissions to HKLM/Software/JavaSoft as mentioned here Java: java.util.Preferences Failing. But this is not something I can require my users to do when they install the program.

So I'm looking for a better solution. My last ditch effort is to simply write to file but I'd like to avoid that. This also seems related I'm trying to use Java Prefences from XML WITHOUT using Windows registry, but I see a Registry-related message but it was down voted without an answer.

At current I suspect a Win8 JVM bug...

Questions

  • Does any one know of a solution that doesn't involve writing files?
  • Why does the same code work perfectly fine in Windows 7 but fails miserably in Windows 8?
like image 244
Emily L. Avatar asked Sep 01 '13 13:09

Emily L.


1 Answers

I recently started noticing a same warning and thought it means that registry cannot be written. But upon closer inspection I noticed that all preferences where successfully updated in HKEY_CURRENT_USER anyways. So I got curious why I'm seeing this warning.

It turned out that the culprit is this static member variable: WindowsPreferences.systemRoot

Looks like Java tries to initialize WindowsPreferences.systemRoot just in case it is used later on by the program, and that initialization obviously fails if the program is not ran as administrator.

Since you're using Preferences.userNodeForPackage(), you will never need the systemRoot, therefore you can safely ignore that warning.

Of course, this is a horrible practice that Java tries to initialize systemRoot when it's not requested.

Update: I tested this problem in various Java versions and concluded that this bug was introduced in Java 1.7.0_21. It worked fine in Java 1.7.0_17 simply because the installer of that version would create the "Pref" folder in registry! Of course even in that version if you were to delete "Pref" from registry then it would stop working, so it was a silly solution on the part of Oracle to begin with. I will fill a bug report.

Update 2: The warning message is not a bug. It seem to be the intended behavior: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6809488

like image 120
Saeid Nourian Avatar answered Sep 28 '22 13:09

Saeid Nourian