File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes Unable to read properties file from Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Unable to read properties file from Servlet" Watch "Unable to read properties file from Servlet" New topic

Unable to read properties file from Servlet

Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
I have some problem in reading the properties file from servlet.I'm using apache tomcat server. Following is the code snippet.I tried putting the properties file in all possible places(classes, webApps, in the context path) but the application failed to load it.It is giving nullpointer exception at prop.load(is);
public class LoadProps {

public Properties getProps() {
String propertyfile = "";
Properties props = new Properties();

try {
is = this.getClass().getResourceAsStream(propertyfile);
}catch (Exception ioe) {
return props;

Appreciate your help
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

This faq entry shows two ways to do it:

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Pedro Erencia
Ranch Hand

Joined: Apr 03, 2008
Posts: 70
Take a look to ServletContext.getResourceAsStream.
If your properties file is located in tomcat-dir/webapps/myapp/WEB-INF/ you could use getServletContext().getResourceAsStream("/WEB-INF/").
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63841

"VSSM Kumar", you have previously been warned on one or more occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it prior to your next post.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Be aware that accounts with invalid display names are disabled.

JavaRanch Sheriff

[Asking smart questions] [About Bear] [Books by Bear]
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
I found some strange things.The servlet class file (not source file) is there in my workspace (c:\workspace\<myappName>\build\classes)and nowhere tomcat server is totally in differnt path.The serlvet mapping in web.xml has the url pattern "/<somepath>".How come at runtime my application could find the servlet class without copying the servlet class file on to the server (i mean deployment)? I'm using Eclipse as IDE.Does Elipse do it automatically? If so, where does it place the class file? I searched my entire C: drive for the class file but could not find it.
I agree. Here's the link:
subject: Unable to read properties file from Servlet
It's not a secret anymore!