0

I have a standard Java application that reads configuration properties at start-up time, which work fine. However, I would like to update the configuration properties at execution time without compiling the code each time. How would I go about doing that.

e.g code:

Properties py = new Properties();
    InputStream ins;
    String prepName = "config.properties";

    ins = getClass().getClassLoader().getResourceAsStream(prepName);

    if (ins == null) {
        System.err.println("Couldn't find the file!");
        return "Error";
    }
    py.load(ins);

    String message = py.getProperty("msg");

resources/config.properties

msg=testMessage

If I want to change message dynamically, how would I do it?

3
  • You could System.setProerty(String key,String value) & System.getProerty(String key) to set all proerties you need to do System.setProerties(py) too. Commented Oct 9, 2014 at 16:43
  • Not sure if I've understood your question, but if you want your app to watch the properties file, and if you're using java 7, you may try something like this - howtodoinjava.com/2012/10/10/… Commented Oct 9, 2014 at 16:48
  • @Leo, thank you for your reference, which is exactly what I need but for Java 6. However, David have suggested a solution for that. Commented Oct 9, 2014 at 17:07

2 Answers 2

2

You can use the setProperty(String key, String value) to change the values at runtime.

py.setProperty("msg", "newValue");
Sign up to request clarification or add additional context in comments.

3 Comments

evanwong, thank you for your prompt response. However, what I want to do is update the config.properties file and would like the application to pick up the new data. is this even possible with .properties method?
@RajK +1 After you update the file, your program has to read it (possibly again)
@RajK, one way to do that is to put the py in some shared state and have a Timer (probably in a separate thread) to refresh the py like in every minute (you define the timer).
0

The WatchService referenced in Leo's comment looks interesting. I have done this pre-Java 7 by using a Properties object and a worker thread that checks the file modification timestamp every 15 seconds (or so). If the timestamp of the file changes, reload the Properties object from the filesystem.

Something like:

Properties py = new Properties();
long lastModMillis = 0L;
long modMillis = file.lastModified() // to get the file modification time

if (modMillis != lastModMillis)
{
    // reload data
    FileInputStream fis = ... 
    py.clear();
    py.load(fis);
    lastModMillis = modMillis;
}

(didn't include worker thread code)

Be sure to consider how you will synchronize things so threads trying to read the data won't collide when the worker thread is reloading the object on file changes.

1 Comment

I, Thank you for above suggestion, which is what I needed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.