5

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?

6
  • 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) Commented Apr 6, 2014 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. Commented Apr 6, 2014 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... Commented Apr 6, 2014 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? Commented Apr 6, 2014 at 12:28
  • Then simply running xdg-user-dir and parsing its output should work for you. Checkout this answer to see how to run an executable from java and also capture the output it returns. Commented Apr 6, 2014 at 12:31

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

2
+50

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.

Comments

1

It seems that xdg-java is abandoned. Instead you could use directories-jvm (https://github.com/dirs-dev/directories-jvm):

dev.dirs:directories:26

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.