Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find directory for "application data" on linux and macintosh

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.

like image 891
CodeBunny Avatar asked Jul 03 '11 04:07

CodeBunny


1 Answers

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.

like image 60
Denis Tulskiy Avatar answered Oct 18 '22 18:10

Denis Tulskiy