I have a properties file in the following path : /WebContent/WEB-INF/systemoperations.properties
I am trying to load the properties file in my servlet init() method.
I tried the following code: Properties properties =null; InputStream inputStream = ClassLoader.getSystemResourceAsStream("WEB-INF/serviceoperations.properties"); properties = new Properties(); properties.load(inputStream);
but this piece of code is throwing a null pointer exception.
We're a friendly group, but we do require members to have valid display names.
Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.
Please edit your profile and correct your display name since accounts with invalid display names get deleted. [ August 12, 2008: Message edited by: David O'Meara ]
Originally posted by Muhammad Safwat: Class loader only loads classes not property files
Not correct. The ClassLoader can be used to find any resource on the classpath and return an InputStream or URL, as is displayed in the examples above.
Originally posted by pheonixashes11: May I know why the previous code didn't work?
The ClassLoader can only load from the ClassPath and the WEB-INF directory is not on the classpath. If you placed the properties in the WEB-INF/classes directory, you could have found the resource as /serviceoperations.properties
While finding resources in web applications via the context is useful in web applications, I prefer to default to the classpath mechanism as it will work in regular applications too.
Originally posted by Bijju Kranthi: How do I load the properties file when I am in a java bean? I donot have a servlet config and context there right.
I assume, your bean is a simple POJO, with getters and setters. Having said that, why do you need to load the property file in your bean? Some other place might be better in this case. What say you.
If you want to load a properties file from a standalone java application you can use Classpath.getResourceAsStream() and the load the properties using Properties.load() Make sure your properties file is in classpath.