You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jacob Kjome <ho...@visi.com> on 2002/12/02 17:58:12 UTC

Re[2]: Tomcat 4.0.3 & getResourceAsStream

Hello Esteban,

It won't work with p.getClass... unless the resource you are trying to
load is in the classpath...meaning it must be within WEB-INF/classes
or WEB-INF/lib/somejar.jar and you wouldn't reference "WEB-INF".  The
classloader knows absolutely nothing about it.

If your resoruce was in the root of WEB-INF/classes (root of the
current classloader... also equivalent to putting the resource in a jar file, but
not inside a package in that jar file), the following will work:
p.getClass.getResourceAsStream("/myResource.properties");

This will work if your resource is located relative to the current
class "p" (same package as the class).
p.getClass.getResourceAsStream("myResource.properties");

This will work to find the resource anywhere in the current
classloader (make sure not to prefix with "/"):
p.getClass.getClassLoader.getResourceAsStream("myResource.properties");

This will work to find the resource even if it isn't in the same
classloader.  For instance, maybe it is in a parent classloader or a
child classloader relative to the current classloader (make sure not
to prefix with "/"):
Thread.currentThread().getContextClassLoader().getResourceAsStream("myResource.properties");

This will work to find the the resource using the servlet context and
fulfill your requirement to put all properties in WEB-INF/conf:
theServletConfig.getServletContext().getResourceAsStream("/WEB-INF/conf/myResource.properties");


For your purposes, you need the last one.  None of the others will
work unless your resource is in the classloader which WEB-INF/conf
isn't.

Jake

Monday, December 02, 2002, 9:59:28 AM, you wrote:

EG> Thanks for your help andreas.

EG> It didn´t work with p.getClass..

EG> i´m trying with getServletContext().

EG> But my idea is to have WEB-INF/conf   to place all .properties files.

EG> is that possible?...

EG> Best regards,
EG> Esteban

EG> ----- Original Message -----
EG> From: "Andreas Probst" <an...@gmx.net>
EG> To: "Tomcat Users List" <to...@jakarta.apache.org>
EG> Sent: Monday, December 02, 2002 12:57 PM
EG> Subject: Re: Tomcat 4.0.3 & getResourceAsStream


EG> Hi Esteban,

EG> try

EG> p.getClass().getResourceAsStream("/WEB-INF/icard.properties");
EG> (inside WEB-INF)
EG> or
EG> p.getClass().getResourceAsStream("/WEB-
EG> INF/classes/icard.properties");
EG> (inside classes)
EG> or
EG> p.getClass().getResourceAsStream("/WEB-
EG> INF/lib/icard.properties");
EG> (inside lib)

EG> If this doesn't work, try
EG> getServletContext().getResourceAsStream(...)
EG> This one will work.

EG> Good luck.

EG> Andreas

EG> On 2 Dec 2002 at 12:33, Esteban González wrote:

>> Hi!
>> I´ve just moved an old app that we had running using Jserv to
>> tomcat4.0.3
>>
>> But i have problems with this
>>     p = new Properties();
>>     InputStream is =
>>     p.getClass().getResourceAsStream("/icard.properties");
>> I keep getting null no matter where i put the icard.properties
>> file.
>>
>> i´ve placed icard.properties inside WEB-INF/lib and
>> WEB-INF/classes and it´s also on the classpath...
>>
>> any workarounds to this issue?...  I´m trying not to use the
>> java.io.* approach...
>>
>> ----------------------------------
>> Esteban González
>>
>> Departamento de Sistemas
>> ASSIST-CARD International
>>
>>
>> --
>> To unsubscribe, e-mail:
>> <ma...@jakarta.apache.org> For
>> additional commands, e-mail:
>> <ma...@jakarta.apache.org>
>>



EG> --
EG> To unsubscribe, e-mail:
EG> <ma...@jakarta.apache.org>
EG> For additional commands, e-mail:
EG> <ma...@jakarta.apache.org>


EG> --
EG> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
EG> For additional commands, e-mail: <ma...@jakarta.apache.org>



-- 
Best regards,
 Jacob                            mailto:hoju@visi.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>