File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes BEA/Weblogic and the fly likes loading properties file within war Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » BEA/Weblogic
Bookmark "loading properties file within war" Watch "loading properties file within war" New topic

loading properties file within war

S Mclatchie

Joined: Jan 23, 2003
Posts: 2
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:
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();
prop.load( this.getClass().getClassLoader().getResourceAsStream("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() );
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.
S Mclatchie

Joined: Jan 23, 2003
Posts: 2
Apologies ... forgot to mention that this is just one version, where I'm trying to use the current class ( to get the ClassLoader. I've tried using the 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

Joined: Jul 18, 2001
Posts: 2712
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

Joined: Dec 29, 2002
Posts: 50
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

Joined: Jan 30, 2003
Posts: 17
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:
subject: loading properties file within war
It's not a secret anymore!