On Wed, 3 Oct 2001, Will Stranathan wrote:
> Date: Wed, 03 Oct 2001 13:58:41 -0500
> From: Will Stranathan <[EMAIL PROTECTED]>
> Reply-To: [EMAIL PROTECTED]
> To: [EMAIL PROTECTED]
> Subject: Re: Finding Resources w/ Tomcat 4.0
>
> Your webapp is in a different classloader. Webapps each get
> their own new classloader.
>
> Use something like this:
>
> ClassLoader loader = getClass().getClassLoader();
>
In addition to this, you should be aware that your proposed code will fail
if your webapp is being run directly from a WAR file rather than from an
unpacked directory. A portable way to do this sort of thing would be:
URL url = getClassLoader().getClass().getResource
("/com/company/app/PropertiesFile.properties");
InputStream in = url.openStream();
Properties props = new Properties();
props.load(in);
in.close();
> Will Stranathan
>
Craig McClanahan
> On Wed, 3 Oct 2001 11:49:29 -0700
> "Haller, Joe" <[EMAIL PROTECTED]> wrote:
> > I have a class that loads initialization properties
> > from a .properties text file in the same directory as the
> > class.
> >
> > The class resides in
> > /TOMCAT_HOME/webapps/myapp/WEB-INF/classes/com/company/app/PropertiesClass.c
> > lass
> > Properties reside in
> > /TOMCAT_HOME/webapps/myapp/WEB-INF/classes/com/company/app/PropertiesFile.pr
> > operties
> >
> > I have a static initializer in PropertiesClass that
> > builds the properties as
> > follows:
> >
> > ...
> > ClassLoader loader = ClassLoader.getSystemClassLoader();
> > URL url =
> > loader.getResource("com/company/app/PropertiesFile.properties");
> > File file = new File(url.getFile());
> > FileInputStream in = new FileInputStream(file);
> > Properties props = new Properties();
> > props.load(in);
> > ...
> >
> > Loading the properties in stand-alone through a test main
> > method executes
> > correctly:
> >
> > i.e.
> >
> > /TOMCAT_HOME/webapps/myapp/WEB-INF/classes> java
> > com.company.app.PropertiesClass
> >
> > However, when I use this class in Tomcat 4.0 the static
> > initializer cannot
> > find
> > the file. The class is being loaded but the
> > .getResource() method in line 2
> > above
> > is not working correctly.
> >
> > Any ideas here?
> >
> > Many thanks.
> > J. Haller
>
>