You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mark <ma...@telocity.com> on 2001/04/17 10:03:40 UTC

Loading Properties from class within a jar file

I am trying to load a properties file from a 
class that is in a jar file that is in the $TOMCAT_HOME/lib directory.

The properties file itself is not in the jar.  It is
in $TOMCAT_HOME/properties, which is
in tomcat's classpath because I added it in
the startup script (I verified that it is listed
in the path at startup).

If I use FileInputStream it works, but only
if I use the full file path.  I obviously would
not like to have to do this.

But if I do the following:

Properties props = new Properties();
try
{
   InputStream is = this.getClass().getResourceAsStream("DB.properties");
   if (is == null)
       throw new Exception("Properties file not found");  
    props.load(is);
}

....


It doesn't work (the exception gets thrown).

Any suggestions on a way to do this that works?

Thanks,

Mark




Re: Loading Properties from class within a jar file

Posted by Pascal Avrilla <pa...@yahoo.fr>.
Hi,

you can put your .properties in your jar (com.here)
and access it by :
InputStream is =
MyClass.class.getClassLoader().getResourceAsStream("com/here/DB.properties");

or use the $TOMCAT_HOME by a System.getPropertie() call.


Mark wrote:
> 
> I am trying to load a properties file from a
> class that is in a jar file that is in the $TOMCAT_HOME/lib directory.
> 
> The properties file itself is not in the jar.  It is
> in $TOMCAT_HOME/properties, which is
> in tomcat's classpath because I added it in
> the startup script (I verified that it is listed
> in the path at startup).
> 
> If I use FileInputStream it works, but only
> if I use the full file path.  I obviously would
> not like to have to do this.
> 
> But if I do the following:
> 
> Properties props = new Properties();
> try
> {
>    InputStream is = this.getClass().getResourceAsStream("DB.properties");
>    if (is == null)
>        throw new Exception("Properties file not found");
>     props.load(is);
> }
> 
> ....
> 
> It doesn't work (the exception gets thrown).
> 
> Any suggestions on a way to do this that works?
> 
> Thanks,
> 
> Mark