2

Lots of applications that we use today can be changed to a users needs in some way.

My goal is as user-friendly application as possible so I want to add a possibility to specify settings like a programs size, log file location, etc.

But how should I actually implement it in Java code?

My current idea is just creating a class that loads these settings by setting some static fields. But for some reason, it feels not right...

What are common ways for this problem? Please, share you experience and knowledge!

Justas S
  • 576
  • 4
  • 10
  • 34

2 Answers2

1

Your idea is correct.

Setting static variables is a great way to do this. Depending on if you want the settings to be persistent (saved and loaded if the program is stopped), its also common to just save the settings to a file. This can be done easily as a csv (Comma Separated Values) or in an XML format.

Sometimes, I use HashMaps for settings, which can easily be dumped and read from files. How do I dump the contents of a hash map?

Community
  • 1
  • 1
1

Try any of the following approached:

  1. Mbeans: Note you would need to expose the MBean - perhaps through HTTP.
  2. A property file that has a listener that gets invoked when it is updated. With Java 7, you have access to listeners on files as well as other options. Please see this link for more details on this: File changed listener in Java
  3. A web ui that takes in the properties.

Note that if your application is trying to self tune or configure or reconfigure itself while it is executing then there are limitations on what properties can be reset and reconfigured. For instance: in Java, the heap size cannot be reconfigured once the VM is initialized. However you could reconfigure the log file location, some of the GC parameters etc.

Community
  • 1
  • 1
Khanna111
  • 3,240
  • 20
  • 23
  • The settings will be only loaded once, on startup. So a file listener is not needed. I will look into the other two options, thanks – Justas S Jun 09 '14 at 19:06