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.
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.
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.