Forums Register Login

Readind property file in web app

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Take a look at the ServletContext api. In particular, the getResourceAsStream(String path) method.

cheers,
ram.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1201 times.
Similar Threads
Read Text File Using Relative Path.
context path in java class
can't use contextPath inside My webservice how to use it.
images in Tomcat
Getting error while reading XML file from initializing bean from servlet.xml
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:11:13.