Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read a property file from WAR  RSS feed

 
sathish kannan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have packed a property file in the WAR under the package WEB-INF as
/WEB-INF/Contant.properties
I want to read and extract the contents of this property file in my java class.
My code is as follows:
String filePath = "/WEB-INF/Contant.properties";
Properties prop = new Properties();
prop.load(new FileInputStream(filePath));

Hashtable ptable = (Hashtable)prop;

is this correct ???
thanks in advance
KSK
 
Karan V Praba
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai,
Please keep your properties file under WEB-INF/classes/ directory of your war file.
( /WEB-INF/classes/Contant.properties ).
 
Nagendra Prasad
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Answer and a Question...

We too keep the properties file in the WEB-INF/ folder. Is there a best practice that suggests that it should be in the WEB-INF/classes directory?
That being the question..
the answer to the sample code:
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't done this myself (reading property files from within a .war file but I believe the reason for placing them into WEB-INF/classes is that you can then read the file using ClassLoader.getResourceAsStream(String path)
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saharass,
Thanks for joining JavaRanch.
Unfortunately your name violates our naming policy. Please take a quick look at the rules and edit your profile accordingly.
Thank you!
 
Shankar Veerashetty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you include the properties file in the classes directory, it will be in the classpath and you can use the Class.getClass().getResourceAsStream()
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!