tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject RE: Loading Properties Files
Date Thu, 05 Dec 2002 03:23:24 GMT


On Wed, 4 Dec 2002, micael wrote:

> Date: Wed, 04 Dec 2002 17:13:16 -0800
> From: micael <caraunltd@harbornet.com>
> Reply-To: Tomcat Users List <tomcat-user@jakarta.apache.org>
> To: Tomcat Users List <tomcat-user@jakarta.apache.org>
> Subject: RE: Loading Properties Files
>
> A .war file is just a wrapper for a web application.  I think he does not
> know what one is.  It is like a .zip or a .jar file.  Do you know that?
>

A WAR file has the same format as a JAR file, but a servlet container --
and most especially the class loader for a webapp that the a servlet
container provides -- very definitely *does* know where to look in a WAR
for unpacked classes/resources (in /WEB-INF/classes) or JAR files
containing classes/resources (in /WEB-INF/lib).  That's why Tomcat is
perfectly capable of running a webapp directly from a WAR, without
unpacking it.

As long as you use the correct resource paths (as shown in the examples),
you can access properties files under /WEB-INF/classes (or from JAR files
in /WEB-INF/classes).  These calls are required to work whether the WAR is
unpacked or not.

Craig


> At 06:56 PM 12/4/2002 -0600, you wrote:
>
> >No, that's not true at all.
> >
> >The examples already given will find properties files for you just fine
> >whether the file is in a directory structure or inside an archive.  How do
> >you think Java loads classes?  It works out of archives, no?
> >
> >here are some various was to access a properties file ( or any resource,
> >for that matter) in whether the app is deployed as a directory or as a
> >.war file (even inside a .jar file in WEB-INF/lib)....
> >
> >This will load a file in WEB-INF/classes/conf or any jar file in the
> >classpath with a package of "conf"...
> >getClass().getResourceAsStream("/conf/db.properties");
> >
> >This will load a file relative to the current class.  For instance, if the
> >class is "org.mypackage.MyClass", then the file would be loaded at
> >"org.mypackage.conf.dbproperties".  Note that this is because we didn't
> >prepend "/" to the path.  When that is done, the file is loaded from the
> >root of the current classloader where this loads it relative to the
> >current class...
> >getClass().getResourceAsStream("conf/db.properties");
> >
> >this will find db.properties anywhere in the current classloader as long
> >as it exists in a "conf" package...
> >getClass().getClassLoader().getResourceAsStream("conf/db.properties");
> >
> >
> >This will find the file in a "conf" directory inside the webapp (starting
> >from the root).  This starts looking in the same directory as contains
> >WEB-INF.  When I say "directory", I don't mean "filesystem".  This could
> >be in a .war file as well as in an actual directory on the filesystem...
> >getServletContext().getResourceAsStream("/conf/db.properties");
> >
> >of course you would probably not want just anyone seeing your
> >db.properties file, so you'd probably want to put in inside WEB-INF of
> >your webapp, so....
> >getServletContext().getResourceAsStream("/WEB-INF/conf/db.properties");
> >
> >If your db.properties exists in another classloader which your app has
> >access to, you can reach it by using:
> >Thread.currentThread().getContextClassLoader().getResourceAsStream("conf/db.properties");
> >
> >That will act similar to getClass().getClassLoader(), but it can see
> >across all available classloaders where the latter can only see within the
> >classloader that loaded the current class.
> >
> >So, as you can see, you have quite a number of options.  There you go.
> >
> >Jake
> >
> >At 03:37 PM 12/4/2002 -0800, you wrote:
> >
> >>If I understand you correctly, the properties file CANNOT be in the war
> >>file, it needs to be external.  Right.
> >>
> >> > -----Original Message-----
> >> > From: micael [mailto:caraunltd@harbornet.com]
> >> > Sent: Wednesday, December 04, 2002 3:25 PM
> >> > To: Tomcat Users List
> >> > Subject: RE: Loading Properties Files
> >> >
> >> >
> >> > Depends upon what you want to do with the properties files
> >> > and how you
> >> > access them.  Some ways of accessing them require that the
> >> > name to access
> >> > be relative to the classpath, others don't.  You are better
> >> > off to learn
> >> > about properties files in this instance.  There is nothing
> >> > peculiar to the
> >> > web-structure that I know of that is important about locating
> >> > properties
> >> > files.
> >> >
> >> > At 03:14 PM 12/4/2002 -0800, you wrote:
> >> >
> >> > >And if you have a .war file?  Then where would you put your
> >> > properties
> >> > >files?
> >> > >
> >> > > > -----Original Message-----
> >> > > > From: Roberto Bouza [mailto:rbouza@efcholdings.com]
> >> > > > Sent: Wednesday, December 04, 2002 2:49 PM
> >> > > > To: Tomcat Users List
> >> > > > Subject: Re: Loading Properties Files
> >> > > >
> >> > > >
> >> > > >
> >> > > >  Thats right.
> >> > > >
> >> > > >  If you don't have a .war file, you can use the classes dir
> >> > > > inside your WEB-INF
> >> > > > dir, and create a new directory like "conf", the put inside
> >> > > > all the properties
> >> > > > files. In that way the ClassLoader looks for  the files in
> >> > > > there when you use
> >> > > > something like this:
> >> > > >
> >> > > >       try {
> >> > > >           Properties props = new Properties();
> >> > > >           InputStream in =
> >> > > > getClass().getResourceAsStream("/conf/db.properties");
> >> > > >           props.load(in);
> >> > > >         ......
> >> > > >
> >> > > >              propertie1 = props.getProperty("propertie1");
> >> > > >
> >> > > >  C'ya
> >> > > >
> >> > > > Quoting Will Hartung <willh@msoft.com>:
> >> > > >
> >> > > > > > From: <echambe1@TXU.COM>
> >> > > > > > Subject: Loading Properties Files
> >> > > > >
> >> > > > >
> >> > > > > > My problem is that the class cannot location my
> >> > > > properties file. I am
> >> > > > > > unable to
> >> > > > > > use other suggested methods that I have noticed on
this
> >> > > > list since those
> >> > > > > > problems
> >> > > > > > involved Properties File within Servlets.
> >> > > > > >
> >> > > > > > After some testing, I determined for some reason the
> >> > > > default directory it
> >> > > > > > is looking
> >> > > > > > for my properties file is the Windows System Directory
> >> > > > (Determined this
> >> > > > > by
> >> > > > > > opening
> >> > > > > > a file in the default directory, outputing something
in
> >> > > > it and searching
> >> > > > > > for the file).
> >> > > > > >
> >> > > > > > Anyone have any ideas on how to solve this problem?
I do
> >> > > > not want to hard
> >> > > > > > code the
> >> > > > > > exact location due to obvious reasons
> >> > > > >
> >> > > > > The problem is that you appear to be loading a file with
an
> >> > > > absolute path,
> >> > > > > versus the common form of load a properties file via the
> >> > > > ClassLoader.
> >> > > > >
> >> > > > > Fumble about with the ClassLoader.getResourceAsStream to
> >> > > > have it hunt down
> >> > > > > your properties file, and then feed that stream to your
> >> > Properties.
> >> > > > >
> >> > > > > public static yourMethod()
> >> > > > > {
> >> > > > >     ClassLoader cl = YourClass.class.getClassLoader();
> >> > > > >     Properties prop = new Properties();
> >> > > > >     prop.load(cl.getResourceAsStream("yours.properties"));
> >> > > > > }
> >> > > > >
> >> > > > > Then, just drop your properties at the right place in your
> >> > > > WARs classes
> >> > > > > area.
> >> > > > >
> >> > > > > Regards,
> >> > > > >
> >> > > > > Will Hartung
> >> > > > > (willh@msoft.com)
> >> > > > >
> >> > > > >
> >> > > > >
> >> > > > >
> >> > > > > --
> >> > > > > To unsubscribe, e-mail:
> >> > > > > <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
> >> > > > > For additional commands, e-mail:
> >> > > > > <mailto:tomcat-user-help@jakarta.apache.org>
> >> > > > >
> >> > > > >
> >> > > >
> >> > > >
> >> > > > --
> >> > > > =             Roberto Bouza Fraga               =
> >> > > > ===================================
> >> > > >       Research & Development Engineer
> >> > > >        Ella Cisneros Fontanals Holdings
> >> > > >  Ph: (305)-860-0116 / Fax:(305)-860-9401
> >> > > > ===================================
> >> > > >        e-Mail:rbouza@efcholdings.com
> >> > > >
> >> > > > --
> >> > > > 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>
> >>
> >>Micael
> >>
> >>-------------------------------------------------------
> >>
> >>This electronic mail  transmission and any accompanying documents contain
> >>information belonging to the sender which may be confidential and legally
> >>privileged.  This information is intended only for the use of the
> >>individual or entity to whom this electronic mail transmission was sent as
> >>indicated above. If you are not the intended recipient, any disclosure,
> >>copying, distribution, or action taken in reliance on the contents of the
> >>information contained in this transmission is strictly prohibited.  If you
> >>have received this transmission in error, please delete the message.  Thank
> >>you
> >>
> >>
> >>
> >>--
> >>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>
>
> Micael
>
> -------------------------------------------------------
>
> This electronic mail  transmission and any accompanying documents contain
> information belonging to the sender which may be confidential and legally
> privileged.  This information is intended only for the use of the
> individual or entity to whom this electronic mail transmission was sent as
> indicated above. If you are not the intended recipient, any disclosure,
> copying, distribution, or action taken in reliance on the contents of the
> information contained in this transmission is strictly prohibited.  If you
> have received this transmission in error, please delete the message.  Thank you
>
>
>
> --
> 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