14

I have a game engine based on LWJGL, and to run it I need to place the required native libraries onto the user's computer. On Windows, I do so by finding the Application Data directory via:

System.getenv("APPDATA");

and everything works easily and nicely. I create a File object, call mkDir if necessary, and write the files if they are not already on the machine.

(Note: the created directory should not be a temporary file, as I would like to save the extracted files for future runs. Additionally, creating this directory gives a simple and easy-to-use folder for saved games and other such data.)

However, I'd like to do something similar if the computer is Macintosh or Linux, but I'm not as familiar with how to do so with those two systems, and I'm not really in a good testing position. My current method to find the target directory is this:

private static String defaultDirectory()
{
    String OS = System.getProperty("os.name").toUpperCase();
    if (OS.contains("WIN"))
        return System.getenv("APPDATA");
    else if (OS.contains("MAC"))
        return System.getProperty("user.home") + "/Library/Application "
                + "Support";
    else if (OS.contains("NUX"))
        return System.getProperty("user.home");
    return System.getProperty("user.dir");
}

So, is this the right way to do this? I'm trying to reach the Application Support on Mac (I've learned this is the equivalent of the AppData folder on windows) and I'm trying to use a similar folder on Linux, but I'm not sure if "user.home" finds the correct one.

CodeBunny
  • 1,991
  • 5
  • 22
  • 32
  • 3
    For linux, you should probably follow the XDG basedir specification. – ergosys Mar 10 '12 at 04:10
  • Does this answer your question? [What is the cross-platform way of obtaining the path to the local application data directory?](https://stackoverflow.com/questions/11113974/what-is-the-cross-platform-way-of-obtaining-the-path-to-the-local-application-da) – Dave Jarvis Dec 26 '22 at 19:29

2 Answers2

6

this should work. just one thing: on linux it is preferred to keep settings in a hidden folder in user directory. So for linux, either put your folder under $HOME/.config, or start the name with the . to make it hidden.

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

As already stated on Linux you should/could put your data in a .directory in the home.

I have however a tip for you: "APPDATA" unfortunately doesn't always find the correct directory across several WindowsVersions. AFAIK the only know way i know how to do this correctly is calling a msdn-function called SHGetFolderPath.

Sample: http://github.com/fab1an/appkit/blob/master/src/main/java/org/appkit/osdependant/OSFolders.java

Fabian Zeindl
  • 5,860
  • 8
  • 54
  • 78
  • Can you elaborate under which circumstances `System.getenv("APPDATA")` will fail? This fairly high rated [answer](http://stackoverflow.com/a/1198954/1804173) does not mention this. – bluenote10 Jun 07 '15 at 12:06
  • See the comments at that question. – Fabian Zeindl Jun 21 '15 at 09:15
  • I was asking because couldn't find a comment regarding _"APPDATA" doesn't always find the correct directory across several Windows versions_. They only mention the "run as" issue. Is this what you mean? – bluenote10 Jun 21 '15 at 09:22