tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Justin Ruthenbeck <just...@nextengine.com>
Subject Re: How to specify the location of a properties file.
Date Wed, 09 Oct 2002 23:22:45 GMT

Hey Shaun --

If the resource name given to getResourceAsStream() isn't found in the web 
application locations (WEB-INF/classes, WEB-INF/lib, Tomcat-specific 
locations), then the system class loader searches for the given resource in 
your classpath one entry at a time.  If the properties file exists in the 
classpath, then it'll be found ... if it doesn't, then it won't.  You can 
always put it in the classes or lib directory (since these are always in a 
J2EE app's classpath), but if you want to keep the configuration files 
outside your app, then you have to use the system classloader.

When you request a resource with the getResourceAsStream() method, Tomcat 
first tries to find the resource using your application's class loader 
(checking /WEB-INF/classes and /WEB-INF/lib).  If not found here, then it 
tries other class loaders up the tree until one finds your resource.  This 
explains why it was found in your classes directory -- "defaults" to that 
-- it's the first place checked for the properties file.  However, any 
directory in your JVM's classpath (such as a "conf" directory) could be 
used for this.

By default, Tomcat (or at least 4.0.x versions) ignores anything you have 
set in your shell CLASSPATH environment variable.  You can change this 
behavior by modifying the $CATALINA_HOME/bin/catalina.sh script to include 
other directories in the system classpath (or your environment's CLASSPATH 
variable as well).  Do this, place your properties file there, and you'll 
find that it gets loaded just fine.

justin

At 02:42 PM 10/9/2002, you wrote:
>Justin
>
>I have written my properties loading like this and I found once that it
>loaded from the classes directory.  I guess the question I have is why it
>defaults to the classes directory and how could I set to something like a
>conf directory below my main webapp root directory.
>
>The other thing is I now can't read the properties file from my classes
>directory for some reason.  I have been playing around with my Tomcat
>configuration. Is there anything in there you need to do to specify where to
>pick up files from that I could have changed?
>
>Thanks in advance.
>
>Shaun
>
>----- Original Message -----
>From: "Justin Ruthenbeck" <justinr@nextengine.com>
>To: "Tomcat Users List" <tomcat-user@jakarta.apache.org>
>Sent: Monday, October 07, 2002 10:20 PM
>Subject: Re: How to specify the location of a properties file.
>
>
> >
> > Niaz ...
> >
> > The idea is to load the properties file like you would any other java
> > resource at runtime ... this is (almost) always better, IMHO, than using
> > something J2EE-specific like initialization parameters to a servlet.
> >
> > The relevant code would look something like this:
> >
> > InputStream inStream = this.getClass().getResourceAsStream("/my.props");
> > Properties props = new Properties(inStream);
> >
> > or
> >
> > Properties prop = new Properties();
> >
>prop.load(this.getClass().getResourceAsStream("/MyProperties.properties"));
> >
> > There was a thread some time ago that went over this.  You can see the
> > details at:
> > http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg63518.html
> >
> > Hope this helps...
> > justin
> >
> >
> > At 01:40 PM 10/7/2002, you wrote:
> > >Justin,
> > >
> > >I am facing the same problem. Your approach seems to be an elegent one.
> > >Would you mind eleborating on the idea a little bit more. Some code
>snippet
> > >would definitely be helpful.
> > >
> > >I thank you in advance.
> > >
> > >niaz.
> > >----- Original Message -----
> > >From: "Justin Ruthenbeck" <justinr@nextengine.com>
> > >To: "Tomcat Users List" <tomcat-user@jakarta.apache.org>
> > >Sent: Monday, October 07, 2002 4:06 PM
> > >Subject: Re: How to specify the location of a properties file.
> > >
> > >
> > > >
> > > > Shaun --
> > > >
> > > > Consider dynamically loading the properties file from your classpath
>using
> > > > a class loader.  This way, you can put the files anywhere you please
>and
> > > > just include that directory in your classpath (or put them someplace
> > > > already in your classpath).  If you need more specifics, let me know
>and
> > > > I'd be happy to help...
> > > >
> > > > justin
> > > >
> > > > At 01:00 PM 10/7/2002, you wrote:
> > > > >I've got a servlet running under Tomcat and I need to read in the
> > >contents
> > > > >of a properties file.  There will be different properties files for
>each
> > > > >system specified using an init parameter.
> > > > >
> > > > >I'm having problems reading this property file at the moment in my
>java
> > > > >class as the way I am doing it at the moment always looks where I
>started
> > > > >Tomcat from i.e the /bin directory.  I can specify a full path to
the
> > >file
> > > > >but this is not very system independent and limits me to either
>Windows
> > >or
> > > > >Unix.
> > > > >
> > > > >What I need is to specify the location of the file relative to the
>webapp
> > > > >directory.  I have tried the url class but it doesn't seem to work,
>or
> > > > >maybe it is working but looking in a different place to where my
> > > > >properties file is.
> > > > >
> > > > >Can anyone suggest what I am doing wrong or provide any help on the
>use
> > >of
> > > > >urls in Tomcat?
> > > > >
> > > > >Thanks
> > > > >
> > > > >
> > > > >Shaun
> > > > >
> > > >
> > > >
> > > > --
> > > > To unsubscribe, e-mail:
> > ><mailto:tomcat-user-unsubscribe@jakarta.apache.org>
> > > > For additional commands, e-mail:
> > ><mailto:tomcat-user-help@jakarta.apache.org>
> > > >
> > >
> > >
> > >--
> > >To unsubscribe, e-mail:
><mailto:tomcat-user-unsubscribe@jakarta.apache.org>
> > >For additional commands, e-mail:
><mailto:tomcat-user-help@jakarta.apache.org>
> >
> >
> > --
> > To unsubscribe, e-mail:
><mailto:tomcat-user-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
><mailto:tomcat-user-help@jakarta.apache.org>
> >
>
>
>--
>To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
>For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>


--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>


Mime
View raw message