Search code examples
javamacoscocoaiopermissions

Writing to `/Library/Preferences` on Mac OS X


I'm writing a Java app that I'm compiling into a .app. I want to write to the system-wide preferences folder in /Library/Preferences, but I can't seem to write there; I assume it's a permissions thing. In what circumstances is an app allowed to write to the "root" Library folder? Where else should I put system-specific (rather than user-specific) config data on OS X?


Solution

  • Writing to root

    The System directory requires root privileges to modify, folders in the Users directory require the permission of either the associated user or an admin, and all other subdirectories to root only require user permissions to modify. In order to create a file or subdirectory directly in root, you need admin privileges. [1]

    Where to place your file(s)

    You likely have problems because the Preferences folder does not need to exist in ~/Library. (~/Library is not supposed to store user-specific data, [1] but Preferences is only meant to store user-specific data. [2])

    In this case, I would suggest storing your configuration files in the same directory as your app, as all files necessary for the app to function are supposed to be there. [2]

    If the file you want to write is not required for the app to function you can place it in Application Support, but you will be required to create the folder if it does not already exist, as well as a subdirectory for your app within it. [2]