Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple, cross-platform to get well-known desktop paths in Java?

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.

like image 584
Michael Ekstrand Avatar asked Oct 26 '12 01:10

Michael Ekstrand


2 Answers

We basically set up a utility class that provides this information, mostly based on the user.home and os.name system property.

We have a series of methods that allow us to build up a path location, something like...

SystemUtilities.getApplicationSettingsPath("Name of my awesome app");
like image 68
MadProgrammer Avatar answered Nov 16 '22 12:11

MadProgrammer


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 here

This class contains some method that might be helpful, such as getHomeDirectory() and getDefaultDirectory(). This class is used by the JFileChooser 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.

like image 27
wattostudios Avatar answered Nov 16 '22 12:11

wattostudios