You live and blinkin learn. I must have used properties files and ResourceBundles a thousand times. The trouble is - it was always in applets. You just fiddle around in JBuilder and eventually find somewhere to put it so that applet.properties ends up in the jar on the classpath.
For the first time I've just tried to use a ResourceBundle and properties file in a servlet. Well, could I get the darned program to pick up the properties file. I was sure that I was doing everything all right. There was a fully qualified path name to a location on the class path:
ResourceBundle rb = ResourceBundle.getBundle("path.to.my.properties.file");
I left out the .properties, like you're supposed to.
I put the thing everywhere, I put it in the hall, I hung it on the wall, but always the MissingResource Exception. All variations of naming conventions were tried.
As ever, you start thinking of corrupted jars at this point in the proceedings when you think that you've tried everything.
Then the tiniest of lights flickered in the noggin: servlets - classloaders - different classloaders - different datatypes...
Sure enough, a look at the Java specs revealed the presence of the following constructor method:
getBundle(String path, Locale locale, Classloader cl)
This sorted it all out:
getBundle("path.to.my.properties.file", Locale.getDefault(), getClass().getClassLoader())
I'm a chemist, ye know.
Posted at 02:46PM Sep 03, 2006 by Brian Peter Clark in Java |