tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject Re: Re[2]: (argh!) RE: Location of application-specific properties files
Date Thu, 08 Aug 2002 08:23:48 GMT

Obviously, i did NOT try your examples - i just tried to answer the
question of Bob (who seems to be satisfied, he does not ask any more),
trying to clear things. Essentially our two answers are the same: usa a
classloader - the confusion was about, where to put the file to be found by
the classloader.


Jens Stutte

                    Jacob Kjome                                                          
                    <        To:     "Tomcat Users List"                  
                    m>                   <>,
                    07/08/2002           Subject:     Re[2]: (argh!) RE: Location of     
                    20.19                application-specific properties files           
                    respond to                                                           
                    "Tomcat Users                                                        

Hello JensStutte,

Ahhh.... I think you must have misunderstood what I was saying.  I am
*not* saying that you would put a class inside WEB-INF.  I'm saying
that if you had servlet specific configuration info, you'd be better
off putting the configuration file inside WEB-INF and accessing it
with the servlet specific

I only pointed that out to make the distinction between loading the
properties off a file path and loading it off a class loader like the
next 3 examples would do.  The 3rd example finding the class in any
of the classloaders provided by Tomcat (except the container specific
classloader) or, if the app isn't running in Tomcat, any classloaders
available to it.

So, to reiterate, the 3 examples following the talk about the
getServletContext().... example are *totally* independent of servlets.

Did you even try them?  I guarantee they work.


Wednesday, August 07, 2002, 2:02:57 AM, you wrote:

Jqi> I did never intend, that you have to include the WEB-INF or classes
Jqi> into the path. The servlet spec says, that the folder
Jqi> will be mounted into the application classpath as well as every jar in
Jqi> "WEB-INF/lib". So a properties file named placed in
Jqi> "WEB-INF/classes" folder (or in any of the mounted jars) will be found
Jqi> inside your application with the simple path "MyProp" or if you use
Jqi> getResourceAsStream() with "". No folders, no magic,
Jqi> servlet dependency. It is the same process like finding a class - and
Jqi> class should never reside directly in the "WEB-INF" folder.

Jqi> Regards,

Jqi> Jens Stutte

Jqi>                     Jacob Kjome

Jqi>                     <        To:     "Tomcat Users List"

Jqi>                     m>
Jqi>                                          cc:

Jqi>                     07/08/2002           Subject:     Re: (argh!) RE:
Location of
Jqi>                     05.49                application-specific
properties files
Jqi>                     Please

Jqi>                     respond to

Jqi>                     "Tomcat Users

Jqi>                     List"

Jqi> Do you literally have the filename as
Jqi> "/WEB-INF/classes/"?

Jqi> The only thing that knows about that is the ServletContext.  If you
were to

Jqi> use that method you'd be better off storing your config file in
Jqi> "WEB-INF" and doing:

Jqi> load(getServletContext().getResourceAsStream
Jqi> ("/WEB-INF/"))

Jqi> Since you want to avoid this so that your app is not servlet specific,
Jqi> can do the following....

Jqi> load(CurrentClass.class.getResourceAsStream("/"));

Jqi> or

Jqi> load(CurrentClass.class.getClassloader().getResourceAsStream
Jqi> (""));

Jqi> or

Jqi> load(Thread.currentThread().getContextClassLoader
Jqi> (""));

Jqi> Notice in all the cases where you are looking in the

Jqi> you do not reference "WEB-INF" or the "classes" directory within
Jqi> WEB-INF.  The classloader doesn't know about those.  The
ServletContext, on

Jqi> the other hand, knows about everything inside the root of the current
Jqi> webapp.

Jqi> That should solve things.

Jqi> Jake

Jqi> At 09:22 PM 8/6/2002 -0400, you wrote:
>>Thanks for the reply.  I understand what you're saying, and I wish it was
>>working for me, but it doesn't.  The ONLY way I've been able to get this
Jqi> to
>>work is by using the servlet context method as mentioned in a previous
Jqi> post,
>>but that's NOT the way I want this to ultimately work.
>>I want to make it as generic as possible without having to rely on
>>in a servlet context to my class.  For example, what if I need access to
>>that file in an application that's not using a servlet?
>>If I have the filename as "/WEB-INF/classes/", I have
>>tried this in one of four different ways so far:
>>1. load(new FileInputStream(filename));
Jqi> ().getResourceAsStream(file
>>3. load(this.getClassLoader().getResourceAsStream(filename));
>>4. load(getServletContext().getResourceAsStream(filename));
>>All of them fail for not finding the file except #4, but that's not the
>>optimal solution as mentioned previously.
>>As an aside, I pulled the "java.class.path" from the System properties to
>>see where it's pointing, and all it has in it is
>>"<CATALINA_HOME>\bin\bootstrap.jar" with no other paths included.
>>-----Original Message-----
>>From: []
>>Sent: Tuesday, August 06, 2002 9:56 AM
>>To: Tomcat Users List
>>Subject: Re: Location of application-specific properties files
>>You must load your properties through some Classloader that is able to
>>locate your properties file.  For example you can use the class
>>PropertyResourceBundle for a real properties file or if you want to
>>some own file format, just call the Thread.getCurrentThread
>>().getContextClassLoader().getResourceAsStream(...) function. Then the
>>WEB-INF/classes folder or some of your application specific .jar files in
>>WEB-INF/lib (if any) is the right place for it (and as far as i know, it
Jqi> is
>>the only standard way, which assures you to find a specific file on every
>>standard J2EE server).
>>To unsubscribe, e-mail:   <
>>For additional commands, e-mail: <

Jqi> --
Jqi> To unsubscribe, e-mail:   <>
Jqi> For additional commands, e-mail: <>

Best regards,

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message