2

I'm working on a game project in Java and I need to store the config.ini file somewhere other than the executable jar location. I know people usually save them inside AppData on Windows, but I'd like to make my game completely portable with no OS restrictions at all. But to do so, I would need to know where to store such file.

I'd like to know where people usually store such files on other UNIX operational systems, mainly Ubuntu or Macintosh (OS X).

I'd also would appreciate if you could explain me a bit more about the directories regarding such OSs, since I do not own any of these systems. What I mean by that is, for instance, in Windows people usually drop their applications on their Desktops, while Mac users put them inside an Applications folder. In Mac's case, would creating a folder inside that Application directory be as bad as creating a folder on a Windows Desktop?

Also, on Ubuntu's case, where would the equivalent to AppData on Windows be located? And what about Mac's case? Would saving screenshots of the game or creating save games inside AppData or equivalents be a bad idea? I don't really want to use the usual Program Files directory.

And finally, a more code-wise issue. I know that in Java you can get Window's AppData location and concatenate such path like the below code:

Path FOO = Paths.get(System.getenv("AppData"), "BAR");

What about Mac and Ubuntu? And how would I be able to check whether the user is using Windows or Ubuntu?


Thank you very much,
-Renato
Renato Geh
  • 78
  • 9
  • 1
    Have you considered the Java Preferences API? http://docs.oracle.com/javase/1.4.2/docs/guide/lang/preferences.html (for the config.ini part of the question, not so much for save games). – Thilo Jul 03 '12 at 01:50
  • Well, I'm not really acquainted with Java Preference API. The thing is, I want the user to actually be able to edit the `config.ini` file in a user-friendly way, just like people usually do when dealing with AAA games. I already have a `config.ini` file ready, as well as a mini-language that checks configurations written in such file and applies them to the game. Plus, as you mentioned, save games will still need to be stored somewhere. But thanks anyway! – Renato Geh Jul 03 '12 at 01:58

1 Answers1

2

For most Unix-like operating systems, including Ubuntu and other Linux distributions, you should follow the XDG Base Directory specification, which describes where user preferences should be stored (namely, the value of the environment variable $XDG_CONFIG_HOME if it exists, falling back to ~/.config/). This directory is intended for configuration. Save games should go in $XDG_DATA_HOME or if not specified ~/.local/share, similarly.

OS X is also Unix-like, but uses its own paths. This is explained at Equivalents of XDG_CONFIG_HOME and XDG_DATA_HOME on Mac OS X?.

Community
  • 1
  • 1
Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
  • Alright, so how would that work code-wise in Java? Would it be something like `System.getenv("$XDG_CONFIG_HOME");`? I know I'm asking a lot but that would be of huge help. Thanks. – Renato Geh Jul 03 '12 at 23:38
  • That should be `System.getenv("XDG_CONFIG_HOME")` (no dollar sign), but then you have to check for `null` and default to `~/.config`. There might be a library that does this for you. – Mechanical snail Jul 04 '12 at 00:47
  • Alright. I'll try that out and Google for more information if I get any doubts. Thanks! – Renato Geh Jul 04 '12 at 01:03