Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving user settings/database/cache... in Java (On every OS)

My Java application is saving stuff in 'user.home' but on windows this does not seem to be the correct path to save application information (as a friend told me). An other option is using the preferences api but it's not possible to set up the hsqldb location using the preferences api. Also, I want all files to be available in the same folder (local database, config, cache, ...).

I'm looking for some example code or a framework that takes care of os-specific stuff.

like image 707
Somatik Avatar asked Dec 02 '08 08:12

Somatik


2 Answers

On my WinXP Pro SP3 system, user.home points to C:\Documents and settings\<username> Lot of applications just store their data there (instead of this path + Application data, but some others go down to there), creating a directory in Unix style, ie. dot + application name (examples: .antexplorer, .sqlworkbench, .squirrel-sql, .SunDownloadManager, .p4qt, .gimp-2.4, etc.).

Looks like a decent, common practice...

like image 190
PhiLho Avatar answered Oct 19 '22 01:10

PhiLho


It's unusual for a window app to save data in user.home but not "wrong". Windows applications love to spread their data all over the place (a bit in the registry, a bit in the application's install directory, a bit in the Windows directory, a bit in System32, a bit here and a bit there). In the end, this makes it impossible to cleanly backup or remove something which results in the famous "Windows rot" (i.e. you have to reinstall every few months).

Anyway. If you really don't want to use user.home (and I see no reason not to), use code like this from Apache commons-lang to figure out if you're running on Windows. If this yields true, pop up a directory selection dialog where the user can specify where they want to save their data.

Save this information in Preferences and use this path the next time when your app is started. This way, users can specify where they want their data and you only have to leave one bit of information in the prefs.

like image 39
Aaron Digulla Avatar answered Oct 19 '22 02:10

Aaron Digulla