简体   繁体   中英

Java: Where to write config without requiring administrative rights

I actually can't believe I'm saying this but since porting my programs to OSX and getting used to permissions, I've realized that what I planned to do on Windows will not work how I want it to. Currently, on windows, my program stores it's setting's in the registry (HKLM) and some user editable resources in a folder next to the program file. For various reasons, I have now decided that the configuration/settings will be stored in a file and the user will be able to in which folder the other resources are kept.

So the question I have now is where to store the configuration file. Obviously it will be updated, but I don't want to program to have to require administrator permissions to run. I would like to offer an option so that all users can use the program (like most programs do), which will of course require Admin, so this leads be onto the second query: where should I store the configuration file (and the folder in which other resources are kept) and how can I detect whether the program has been installed for all users or just one!

Thanks in advance

PS If you didn't guess, the program is written in Java so I would like to know how to programatically get the location you suggest as well please.

Its normal practice in *nix compatible programs to store information in folders starting with name . in the home directory of the users like,

.bash_history
.bashrc

You could use the same on OSX in my opinion and create a directory say,

.myapp

You can store any number of files with any format under that directory.

To get the location of the folder, you can do

String homeDir = System.getProperty("user.home");
File myAppDir = new File(homeDir, ".myapp");

That is roughly the code that can get you your custom config directory for your app.

Please not that dot files / folders are somewhat similar to hidden folders in windows. Your File Manager will not generally show these files / folders by default.

To identify if the program is installed for all the users or not, you could create the configuration at some administrator (root) controlled location like /etc (not sure about Mac) The user configuration can always override the default config. There could be a better way to handle this though.

On both windows and unix, User(usually) has a (home)folder to which it has full permissions. You may create a directory in the home folder and have your user configuration files reside there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM