• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

loading properties file within war

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>?
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic