4

I'm trying to follow XDG directory specification on my Java application. I have already used it for application data:

 protected String getDefaultDataDir() {

    String rootPath = System.getenv("XDG_DATA_HOME");

    if(rootPath == null) {
        rootPath = System.getProperty("user.home")+S+".local"+S+"share";
    }

    return rootPath+S+Pinocchio.PLATFORMNAME;
 }

( S is a constant containing System.getProperty("file.separator"))

I want now to store documents on the user documents' folder. But I don't have an environment variable named XDG_DOCUMENTS_DIR. I have the ".config/user-dirs.dirs" file that have that XDG configuration, and the "xdg-user-dir DOCUMENTS" which directly returns that path.

Making things worse, the default documents directory depends on the user locale.

Which is the best way to use the configured Documents directory on a java application?

Flamma
  • 258
  • 4
  • 15
  • You mention "default documents directory depends on the user locale". Where is this info stored? i.e. the current user locale and also the path for each locale etc. (am simply trying to understand exactly what the problem is) – TheCodeArtist Apr 06 '14 at 11:17
  • I'm afraid you'd either have to use `ProcessBuilder` to run `xdg-user-dir` or parse `.config/user-dirs.dirs` yourself. – Denis Tulskiy Apr 06 '14 at 11:21
  • @DenisTulskiy You are right. My first reaction was also to suggest running `xdg-user-dir` from the code. However am still wondering how the localisation i.e. **user locale** "business" fits in. Hence waiting for more details... – TheCodeArtist Apr 06 '14 at 11:29
  • @TheCodeArtist xdg-user-dir and xdg-user-dirs-update manage that. On my Debian machine, those default dirs are listed on /etc/xdg/user-dirs.defaults . Then each path element is translated by the applications using their i18n files, for instance /usr/share/locale/es/LC_MESSAGES/xdg-user-dirs.mo for spanish translation. Is that what you were asking? – Flamma Apr 06 '14 at 12:28
  • Then simply running `xdg-user-dir` and parsing its output should work for you. Checkout this [**answer**](http://stackoverflow.com/a/5711150/319204) to see how to run an executable from java and also capture the output it returns. – TheCodeArtist Apr 06 '14 at 12:31
  • @TheCodeArtist Thanks. I already implemented it that way. I was just wondering if there was a better way that didn't rely on calling a native application. It's quite strange XDG is not supported on Java, IMO. – Flamma Apr 06 '14 at 12:35

2 Answers2

4

While looking for an answer for this, I found xdg-java. I haven't tested it yet but seems to do exactly what you (and I) want.

It doesn't yet implement all the specifications but it does implement the ones I need for now and I would guess the author would be receptive to patches.

carandraug
  • 12,938
  • 1
  • 26
  • 38
2

On Gnome there is java-gnome support library, it is doing native calls for better integration with gnome. It can get path of user dirs via calls to Glib.getUserSpecialDir. Haven't tried this one myself, might be worth a try if you are planning to use more desktop-integrated features in your program.

Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68