1

Earlier I put my properties file within my classpath src/. Now I would like to put it within a folder called config/. This way the end users can actually modify the file by themselves.

However now my code below does not work anymore

ResourceBundle.getBundle("Messages", Locale.getDefault());

What I meant by the code doesn't work anymore is, after I deploy the application and I modify the Messages_en_US.properties the changes do not take place.

How can I achieve what I want ? Should I use getBundle at all ?

EDIT

I have added config folder into the classpath as well, but I am not sure if this is relevant. This is a plain Java application where I am not using Maven or any building tools.

enter image description here

abiieez
  • 3,139
  • 14
  • 57
  • 110
  • 3
    You need to say how you're building your code and where exactly this `config` directory is. Generally speaking, your bundle will be looked for in the root of the classpath, so you may need to specify `config.Messages` or similar. The usual approach with a build system like Maven is to put resources such as properties files in a separate directory tree (like `src/main/resources`), and they end up at the root of the classpath. – chrylis -cautiouslyoptimistic- Feb 22 '14 at 12:42
  • How you're building, and more importantly, how you're running it. Show us where the various files are located, from where you execute the java command, and which command you're executing. – JB Nizet Feb 22 '14 at 12:44
  • I've updated with a screenshot. – abiieez Feb 22 '14 at 12:45
  • OK. So you have an Eclipse project, and defined config as a source folder, is that right? What happens precisely when you execute the above code? What is your default locale? – JB Nizet Feb 22 '14 at 12:50
  • When I edit the properties file within the `config` the changes does not take place. The default locale is `en_US` – abiieez Feb 22 '14 at 12:51
  • So the bundle is loaded correctly, but contains obsolete values, is that right? Then you should probably clean your project, and make sure you haven't left the same properties files in the src folder as well. – JB Nizet Feb 22 '14 at 13:03
  • Not really, because the jar file also contains the properties file. If I remove the `config` folder from the classPath then I get `java.util.MissingResourceException` – abiieez Feb 22 '14 at 13:11
  • okay I got it working based on the link given by @Daniel Ruf.. Should I delete this question ? – abiieez Feb 22 '14 at 13:13
  • possible duplicate of http://stackoverflow.com/questions/1172424/how-to-load-a-resource-bundle-from-a-file-resource-in-java –  Feb 22 '14 at 13:14
  • good to know =) I have added the comment and the answer, so the question is linked to the other SO question ;-) –  Feb 22 '14 at 13:45

1 Answers1

0

By default, a ResourceBundle is only loaded when it is first requested, and reused for subsequent requests. You can throw away the cached ResourceBundles with ResourceBundle.clearCache();

Additionally, by default, ResourceBundles are loaded from the classpath. You must therefore ensure that the classloader in question does not cache the resource either. Or you can provide your own ResourceBundle.Control to load the properties file by whatever means you prefer.

meriton
  • 68,356
  • 14
  • 108
  • 175