• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loading properties file within war

 
S Mclatchie
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Not sure if this is the correct forum, but is my first post - so feel free to redirect me...
Here's the problem.
I've created a webapp packaged as a war, to run it needs to access a property file which contains information for the different app servers it may be deployed on (Websphere, WebLogic, Tomcat). I can get it to load this property file fine if I hard code the location for each app server. However, what I'd like to do is use the ClassLoader to get the Resource as a Stream. I can't get it to find the properties file.
Here's the Package structure:
index.jsp
properties/projectweb.properties
WEB-INF/classes/projectweb/servlet/BuildProjectWeb.java
WEB-INF/classes/projectweb/servlet/ResourceAnchor.java
ResourceAnchor is an empty class with a simple constructor with no implementation. It is used so the ClassLoader can get reference to it's location.
Here's the code:
//Load Properties File
ClassLoader cl = ResourceAnchor.class.getClassLoader();
prop = new Properties();
try
{
System.out.println(this.getClass().getResource("projectweb.properties"));
prop.load( this.getClass().getClassLoader().getResourceAsStream("properties/projectweb.properties") );
}
catch( IOException ioex )
{
System.err.println( "*** IOException : failed to load properties: " + ioex.toString() );
}
catch (Exception ex )
{
System.err.println( "*** Exception : failed to load properties: " + ex.toString() );
ex.printStackTrace();
}
Can anyone see why it always catches the Exception (not the IOException) and provide any help as to how I might fix this. Been trying for over a day now and it's getting to me!
Any help appreciated.
Stuart.
 
S Mclatchie
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies ... forgot to mention that this is just one version, where I'm trying to use the current class (BuildProjectWeb.java) to get the ClassLoader. I've tried using the ResourceAnchor.java as well..
Also the line
prop = new Properties();
It is declared earlier so no worries there
Does the war have to be exploded for it to find the properties file>?
 
Chris Mathews
Ranch Hand
Posts: 2712
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suggestion is to put the properties file on the Application Server CLASSPATH.
Alternatively, depending on where the properties are used, you could set the properties in the web.xml as initialization parameters.
 
Vikalp Singh
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even we faced same problem in our application,
where we have to access the property files from different servers
what we have done --
we defined one system / envirnment variable ,
from that variable we r getting the files location and we can access that peroperty files from all servers.
 
Kieran Bee
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the properties file in the web app's classpath, i.e. WEB-INF/classes (and from there add a properties directory if you don't want to change your current code)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic