I’m using Tomcat 5.5 as my servlet container. My web application deploys via .jar and has some resource files (textual files with strings and configuration parameters) located under its WEB-INF directory. Tomcat 5.5 runs on ubuntu linux. The resource file is read with a file reader:
fr = new FileReader('messages.properties');
The problem is that sometimes the servlet can’t find the resource file, but if i restart it a couple of times it works, then again after some time it stops working. Can someone suggest what’s the best way of reading resource strings from a servlet? Or a workaround for this problem? Putting the resource files under WEB-INF/classes doesn’t help either.
I’m guessing the problem is you’re trying to use a relative path to access the file. Using absolute path should help (i.e. ‘/home/tomcat5/properties/messages.properties’).
However, the usual solution to this problem is to use the getResourceAsStream method of the ClassLoader. Deploying the properties file to ‘WEB-INF/classes’ will make it available to the class loader and you’ll be able to access the properties stream.
Untested proto-code: