I'm looking for a simple, cross-platform way, on the JVM, to get the appropriate directories for storing application settings and cache data. E.g., on Linux under the XDG specs:
- config in
~/.config/appname
- cache in
~/.cache/appname
Windows stuff goes in C\Users\user\Application Data
or some such (on Win 7); I gather that Mac puts stuff under ~/Library/Application Settings
.
Is there a cross-platform call to get these path locations? I haven't been able to find one, though I find several questions asking about it. Is there some library that has implemented the correct logic for most common desktop platforms?
Worst case, I can just look up user.home
and pretend everything is an XDG-based Linux environment. But if it's easy to get the right directories for the user's OS, I'd like to do so.
The
FileSystemView
class is the only one I know that contains access to some general directories like this, and is cross-platform. Refer to the documentation hereThis class contains some method that might be helpful, such as
getHomeDirectory()
andgetDefaultDirectory()
. This class is used by theJFileChooser
for Swing interfaces. I know it doesn't directly point to the config and cache directories, but it might be a step in the right direction for you.For my own apps, I take the next-best approach by trying to make all the config and settings contained within the application itself - ie. create a "config" directory under the directory where the application is installed. Even though its not a central location for config for the whole OS, at least it keeps it within the java application context.