views:

1281

answers:

5

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.

+1  A: 

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.

Aaron Digulla
+1  A: 

For an application "foo" I'd create a directory named ".foo" inside user.home. For Windows it will look slightly strange, but almost noone ever looks in that directory (and it's filled with obscure directories anyway) and on Linux/Solaris/... it will result in a hidden directory that doesn't clutter the users home directory visually.

Joachim Sauer
Lots of apps do this, pidgin etc
SCdF
A: 

The problem is not Wiindows but the standard java setup.

Long Discussion http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4787931>Here

I would advise picking this up on a '-D MYLOC=%USERPROFILE%' property on the command line.

Note that you will only get a "USERPROFILE" if the user did a desktop login, this does not get set if the user logged in remotly with citrix or similar or via ssh, also, coprporate desktops mess around with this setting and may set it to something unusable.

James Anderson
+3  A: 

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...

PhiLho
A: 

dynamically read (from your code) the value of APPDATA environment variable, and store your config files in %APPDATA%\.myapp\config

==> value is platform dependent, do not use any hard-coded paths, always read the env. var.

orcus