Impossible to store boolean preferences to Windows 10 registry from Java, while integer and String are correctly stored

0

I'm using the example from https://www.vogella.com/tutorials/JavaPreferences/article.html with the following code:

import java.util.prefs.Preferences;

public class PreferenceTest {
  private Preferences prefs;

  public void setPreference() {
    // This will define a node in which the preferences can be stored
    prefs = Preferences.userRoot().node(this.getClass().getName());
    String ID1 = "Test1";
    String ID2 = "Test2";
    String ID3 = "Test3";

    // First we will get the values
    // Define a boolean value
    System.out.println(prefs.getBoolean(ID1, true));
    // Define a string with default "Hello World
    System.out.println(prefs.get(ID2, "Hello World"));
    // Define a integer with default 50
    System.out.println(prefs.getInt(ID3, 50));

    // now set the values
    prefs.putBoolean(ID1, false);
    prefs.put(ID2, "Hello Europa");
    prefs.putInt(ID3, 45);

    // Delete the preference settings for the first value
    prefs.remove(ID1);

  }

  public static void main(String[] args) {
    PreferenceTest test = new PreferenceTest();
    test.setPreference();
  }
}

and below is the output of the consequent runs:

1) initial run to save the preferences:

true Apr 23, 2019 10:56:22 PM java.util.prefs.WindowsPreferences Hello World WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5. 50

2) the second run that already reads the values written at the first run:

true Apr 23, 2019 10:56:57 PM java.util.prefs.WindowsPreferences Hello Europa WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5. 45

You may note that the int and the String values are retrieved well and not substituted by the default values, while the boolean value isn't retrieved and is substituted to the default one (i.e. both outputs give true there while the expectation was to achieve false at the second run).

java
registry
preferences
asked on Stack Overflow Apr 23, 2019 by Ilya Yevlampiev

1 Answer

0

I was wrong here: I have missed the fact that ID1 key is removed from the Preferences:

// Delete the preference settings for the first value
    prefs.remove(ID1);

if I comment it out, everything works as expected.

answered on Stack Overflow Apr 24, 2019 by Ilya Yevlampiev

User contributions licensed under CC BY-SA 3.0