Can't write Java preferences in Windows 10

1

I'm trying to run a program that looks for Java preferences in Windows 10 using JDK 10.0.2. If it finds them under the system node, it tries to copy them to the user's own node and then the user can change them to their liking. I'm not having any luck in either reading or writing preferences. I've tried a few different JDKs at version 8 and then installed 10 on the theory that it might work better but it didn't.

My program is an old game that I wrote several years ago. It worked fine the last time I ran it in 2012. (At that point, I was probably running JDK 6 and Windows XP.)

I found some code that uses preferences, which is a good bit simpler than my own, in another StackOverflow discussion, modified it a bit, and tried to run it to see if I could come up with an easily reproducible example:

    import java.util.prefs.BackingStoreException;
    import java.util.prefs.Preferences;

    public class Prefs01 {

    public static void main( final String[] args ) throws BackingStoreException {

    Preferences systemRoot = Preferences.systemRoot(); 

    Preferences preferences = systemRoot.node("fuzz"); 

    systemRoot.put( "foo", "bar" );

    systemRoot.put( "baz", "lolz" );
    System.out.println( "-------------------------------" );

    String[] keys = preferences.keys();
    for( String key : keys ) {
        System.out.println( key );
    }

    System.out.println( "-------------------------------" );

    keys = systemRoot.keys();
    for( String key : keys ) {
        System.out.println( key );
    }
  }
}

Results

Preferences systemRoot = Preferences.systemRoot(); 

Debugger shows that systemRoot is "/" after this statement; no error so far.

Preferences preferences = systemRoot.node("fuzz"); 

Console produces this message:

Oct 01, 2018 5:05:24 PM java.util.prefs.WindowsPreferences <init>
WARNING: Could not create windows registry node Software\JavaSoft\Prefs\fuzz at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.  

systemRoot.put( "foo", "bar" );

Console produces this message and stacktrace:

Oct 01, 2018 5:09:38 PM java.util.prefs.WindowsPreferences openKey WARNING: Could not open windows registry node Software\JavaSoft\Prefs at root 0x80000002. Windows RegOpenKey(...) returned error code 5. Exception in thread "main" java.lang.SecurityException: Could not open windows registry node Software\JavaSoft\Prefs at root 0x80000002: Access denied at java.prefs/java.util.prefs.WindowsPreferences.openKey(WindowsPreferences.java:553) at java.prefs/java.util.prefs.WindowsPreferences.openKey(WindowsPreferences.java:515) at java.prefs/java.util.prefs.WindowsPreferences.openKey(WindowsPreferences.java:501) at java.prefs/java.util.prefs.WindowsPreferences.putSpi(WindowsPreferences.java:652) at java.prefs/java.util.prefs.AbstractPreferences.put(AbstractPreferences.java:263) at Prefs01.main(Prefs01.java:13)

Based on the error messages I'm getting, I am under the impression that I have some kind of a Windows authorization issue: I'm not authorized to do what I'm trying to do in the Windows Registry. This makes no sense to me because there is only one UAC on this machine and it is an administrator; I don't know of any way to run this program under a different UAC even if there were another UAC on the machine.

Can anyone shed some light on what is going wrong here and what I need to do to fix it?

java
windows-10
asked on Stack Overflow Oct 1, 2018 by Henry • edited Oct 2, 2018 by Harry Coder

1 Answer

0

As some of the commenters have stated, running "As Administrator" will probably solve the problem.

However, the root cause of your issue is given in this error, and in your code:

Preferences systemRoot = Preferences.systemRoot(); 
...
WARNING: Could not open windows registry node Software\JavaSoft\Prefs at root 0x80000002

In the current JDK implementations, systemRoot is equivalent to HKEY_LOCAL_MACHINE (as is 0x80000002). By default, users don't have write access here without going through UAC.

To resolve this, you'd be best to change your code to use Preferences.userRoot(), which accesses registry nodes under HKEY_CURRENT_USER, which is normally writable.

answered on Stack Overflow Mar 31, 2020 by Rocketeer

User contributions licensed under CC BY-SA 3.0