2

I've created an application that is going to be run on Windows, Mac OX and Linux. I need to be able to store and read user settings on the fly.

User settings take the form of strings and a key and value pair would work well.

I'm currently using a properties file however this can't be written too on the fly within a JAR.

I'm struggling to find alternatives, what are the options to be able to do this?

Jack Trowbridge
  • 3,175
  • 9
  • 32
  • 56

1 Answers1

2

You can save the properties file in the user home directory using System.getProperty("user.home") assuming the security manager, if any, allows it. Using properties files to save preferences allows editing the preferences from outside the application.

Another option is to use the Java preferences API for a transparent and platform-independent way of persisting the preferences.

M A
  • 71,713
  • 13
  • 134
  • 174
  • I get the following error using Java preferences API : WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5. – Jack Trowbridge Dec 24 '14 at 19:47
  • I also get a java.lang.NullPointerException when trying to save a file to "user.home". – Jack Trowbridge Dec 24 '14 at 19:47
  • http://stackoverflow.com/questions/16428098/groovy-shell-warning-could-not-open-create-prefs-root-node may help. – M A Dec 24 '14 at 19:59
  • Under MacOS, the preferred location for configuration files is `~/Library/Application Support/{app name}`, under windows it's `~\AppData\Local\{app name}` or `~\AppData\Roaming\{app name}` depending on your needs – MadProgrammer Dec 24 '14 at 22:02
  • @JackTrowbridge The error would suggest that you do not have adequate security permissions to write to the registry... – MadProgrammer Dec 24 '14 at 22:03