The road so far….

November 12, 2010

Use java.util.prefs.Preferences instead of java.util.Properties

Filed under: java — Tags: , — Rahul Sharma @ 10:07 pm

A typical installer for an application needs to ask the user a couple of options and some of these are configuration questions e.g. the port on which the application should run, how it should run etc. The application has to remember these options and use them in every run. Standard manner of solving such a problem is to write these options in a properties file which can be loaded at the start-up of the application. But then again the problem shifts to some other area i.e.  remember the install path and then load the required properties file from the installed path.

Remembering installed path can be solved by setting an environment variable e.g. MYAPP_HOME. The variable can be initialized with the required value  while installing  so that every time the application gets loaded the variable will be set. This is a typical solution that is employed in most of the projects.

The Other Solution

The Preferences API that is provided JDK can be used to solve this typical problem. Preferences work just like properties but they are persistent unlike Properties. At the back, when a preference is written it gets stored to a backing store. When you ask for the preference, the value  is then  loaded from this store. On a typical Windows machine the default store is Windows registry but the store is configurable and you can change it to what ever you like e.g. a file.

Writing a preference is straight forward. Unlike properties which are String based key-value pairs the preferences have keys that are Strings but you can store values of all basic types e.g. long, boolean, double etc.

public class StaticPreferenceFactory {
    public static void main(String args[]) throws Exception {
        Preferences prefsRoot = Preferences.userRoot();
        Preferences myPrefs = prefsRoot
        myPrefs.put("fruit", "apple");
        myPrefs.putDouble("price", 40);
        myPrefs.putBoolean("available", false);
        return prefsRoot;

Just like we have system variables and user variables. There is a system preference node that  you can get by calling  systemRoot() and there is a user preference node that you get calling userRoot() node.  Once a preference is stored in a userNode it is not accessible to other users of the system just like user variables. You can clear the preferences written by calling the clear() API.

Retrieving a preference is also straight forward just like properties. The get API here takes two arguments the key, to be found, and default value, incase the value is not found.

public class UsePreference {
    public static void main(String args[]) throws Exception {
        Preferences myfilePrefs = Preferences.userRoot();
        myfilePrefs = myfilePrefs
        System.out.println("finding fruit:"
                + myfilePrefs.get("fruit", "not found") + " available :"
                + myfilePrefs.getBoolean("available", true));

Spring also provdes PreferencesPlaceholderConfigurer that  can be used to load preferences.

<bean id="preferencePlaceHolder"
		<property name="userTreePath" value="com.myapp.preference.staticPreferenceLoader" />
	<bean id="myEntity" class="info.dependencyInjection.spring.factory.MyEntity">
		<property name="value" value="${fruit}" />

For our installer problem we can store all our configuration options in Preferences while installing and the application will be only concerned about reading these values. This way we can avoid all the pains of writing to environment variables and making sure that we load the proper variables every time.



  1. Very useful, thanks.

    Comment by Eugene — November 16, 2010 @ 12:56 am

  2. Good One.. !! Simple yet often forgotten one !! Thanks

    Comment by Veerabahu — November 16, 2010 @ 10:59 am

  3. thanks. very useful information. clear and simple

    Comment by joseph — November 16, 2010 @ 12:27 pm

  4. Thaks, it was useful.

    Comment by Andrei Solntsev — November 17, 2010 @ 2:53 am

  5. I have a problem with the overall premise of your article but I still think its really informative. I really like your other posts. Keep up the great work. If you can add more video and pictures can be much better. Because they help much clear understanding. 🙂 thanks Tianyahaijiao.

    Comment by Tianyahaijiao — November 18, 2010 @ 11:29 am

  6. You did a first-rate job by sharing this. Thanks a lot!!

    Comment by Gift Sam — November 19, 2010 @ 9:15 pm

  7. Cool added to my bookmarks for feature reference. Thanks for sharing

    Comment by Eswar — November 20, 2010 @ 3:08 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: