Search code examples
javamacosfilefile-location

OSX/MacOS equivalent of Windows %PROGRAMDATA% environment variable


I'd like my application to write out some data (e.g. configuration, cache, licences etc) to a shared, all-user accessible location.

On Windows I've done this to %PROGRAMDATA%.

On MacOS, my research has suggested using the Library/Application Support directory.

While the user-specific ~/Library/Application Support works, the non-user specific /Library/Application Support doesn't seem to be write-accessible?

Example:

Files.writeString(Paths.get("/Library/Application Support/myfile.txt"), "hello world")

Throws Exception:

java.nio.file.AccessDeniedException: /Library/Application Support/myfile.txt

Is there a better destination for this kind of data?


Solution

  • There isn't really a macOS equivalent of this. Probably the best place to store something like this is in /Users/Shared (see this thread on the Apple developer forum), but it's not entirely ideal either.

    One thing to be aware of is permissions: /Users/Shared has the "sticky" bit set, meaning that users can't delete (or rename) files they don't own. You'll probably want to create a subdirectory for your app's files, so you can control access to the files via that. You'll also have to pay attention to file permissions, since the default is for the owner (the user that created a file or folder) to have full access, and everyone else read-only. You probably want to do something like setting the group to "staff" and grant write access to the group as well as the owner.

    Another is a consequence of that: everyone (and pretty much everything) on the computer has write access to it, and can make whatever kind of mess they want to. Access is not restricted to your app (although sandboxed apps generally won't have access). See Quinn “The Eskimo!”'s warnings in the dev thread.