f4lco
f4lco

Reputation: 3814

Where to store preferences in a Java application?

I need just a solution to retrieve a path for storing desktop application settings (e.g. a sqlite database) that meets following needs:

I've spent a lot of time googling and experimenting with getting the codebase path via X.class.getProtectionDomain().getCodeSource().getLocation() and java.util.pref.Preferences class, but return values gave sometimes different results, returned null or just "/" as path.

"Cross Platform" is kind of hard to define. A solution called "best practice for each platform" won't be easy to achieve. Making a difference between user-specific and shared data would be sensible.

Upvotes: 1

Views: 2837

Answers (2)

user238033
user238033

Reputation:

System.getProperty("user.dir") returns a path to the user directory for most modern systems.

Upvotes: 0

Bozho
Bozho

Reputation: 597036

java.util.prefs.Preferences is the proper way to do it. It works perfectly fine (I've used it in a desktop application without any problems). It is cross-platform and is available without any extra jars. It also differentiates between user-specific and global data. Perhaps shed some light on your particular problems with it (or ask another question)

Of course, you can use java.util.Properties as well, and store files in System.getProperty("user.dir").

Upvotes: 9

Related Questions