SOCKETTechnical blog for SOCKEThttp://www.agbooth.com/SOCKETBlog/www.socketelf.org_8080/roller/socket/feed/entries/atom2008-05-06T18:15:30+01:00Apache Roller (incubating)http://www.agbooth.com/SOCKETBlog/www.socketelf.org_8080/roller/socket/entry/lost_property.htmlLost PropertyBrian Peter Clark2006-09-03T14:46:05+01:002007-09-10T19:20:06+01:00
<p>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.</p>
<p>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:</p>
<p>ResourceBundle rb = ResourceBundle.getBundle("path.to.my.properties.file");</p>
<p>I left out the .properties, like you're supposed to.</p>
<p>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. </p>
<p>As ever, you start thinking of corrupted jars at this point in the proceedings when you think that you've tried everything.</p>
<p>Then the tiniest of lights flickered in the noggin: servlets - classloaders - different classloaders - different datatypes...</p>
<p>Sure enough, a look at the Java specs revealed the presence of the following constructor method:</p>
<p>getBundle(String path, Locale locale, Classloader cl)</p>
<p>This sorted it all out:</p>
<p>getBundle("path.to.my.properties.file", Locale.getDefault(), getClass().getClassLoader())</p>
<p>I'm a chemist, ye know.</p>