Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Readind property file in web app

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the ServletContext api. In particular, the getResourceAsStream(String path) method.

cheers,
ram.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
brevity is the soul of wit - shakepeare. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic