5

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?

Jakg
  • 922
  • 12
  • 39

3 Answers3

3

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.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
0

You're getting that error because you're trying to write a file to a directory which is owned by root. You could change the file permissions on that directory such that the user executing your program has write access to that one file or to its containing directory.

Eric Green
  • 1,151
  • 9
  • 17
  • Thanks, but that solution is user-specific, which I've already got working - i'm looking for an "all users" solution. – Jakg Dec 19 '19 at 00:43
  • If you want this for all users, you could change the file permissions on that directory such that the user executing your program has write access to that file or its containing directory. – Eric Green Dec 19 '19 at 00:46
0

Part of my requirement was to store configuration data.

The Java preferences API - https://www.vogella.com/tutorials/JavaPreferences/article.html - is a far more sensible way for me to acheive this, as the JRE abstracts how the preferences are stored in the filesystem.

Doesn't work for cache data, though (specifically - cached files that are virtually permanent rather than temporary).

Jakg
  • 922
  • 12
  • 39