This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com