Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Readind property file in web app

 
Babar Qadri
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the ServletContext api. In particular, the getResourceAsStream(String path) method.

cheers,
ram.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Babar Qadri
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic