aspose file tools*
The moose likes Servlets and the fly likes Readind property file in web app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Readind property file in web app" Watch "Readind property file in web app" New topic
Author

Readind property file in web app

Babar Qadri
Ranch Hand

Joined: Oct 12, 2004
Posts: 51
I am having hard time in reading the property file from my plain java class in a web application.

Does relative path, in order to read a file, not work in a web application?

I am able to read that file by defining an absolute path but relative path is not working. I dont want to use servletContext.getRealPath() and then concat that to my relative path. Is there any other way around?

Thanks.


babar
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

Take a look at the ServletContext api. In particular, the getResourceAsStream(String path) method.

cheers,
ram.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Babar Qadri:
I am having hard time in reading the property file from my plain java class in a web application.

Does relative path, in order to read a file, not work in a web application?

I am able to read that file by defining an absolute path but relative path is not working. I dont want to use servletContext.getRealPath() and then concat that to my relative path. Is there any other way around?

Thanks.


Many times i face the same problem while reading the property file from a application or a web application.
I use the following methods for reading the property file in different senarios.

1> if I have a class at {MY-WEB-CONTEXT}/WB-INF/classes or {MY-WEB-CONTEXT}/WB-INF/lib , and the class from which i want to load the property file is RahulExample.class then i would use
...
ClassLoader cl = RahulExample.class.getClassLoader();

InputStream in = cl.getResourceAsStream(PROPERTY_FILE_NAME);

2>If i have a standalone application then put the property file in the classpath then can use the system classloader to load the property file.

Cheers!
--Rahul


Rahul Bhattacharjee
LinkedIn - Blog
Babar Qadri
Ranch Hand

Joined: Oct 12, 2004
Posts: 51
Ok. I dont have servletContext object in my class, nor my class is a servlet. Now how to read/load the property file from my plain java class by using relative path?

[Edited]
Oops! I didn't say thanks.

Thanks folks for your help.
[ November 29, 2005: Message edited by: Babar Qadri ]
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

As the post before your last one suggested, put it somewhere in the classpath (WEB-INF/classes) and use the getResourceAsStream() method of java.lang.Class

ram.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Readind property file in web app