On Mon, 29 Jul 2002, Chris Ruegger wrote:

> Date: Mon, 29 Jul 2002 13:30:09 -0400
> From: Chris Ruegger <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>
> To: Tomcat Users List <[EMAIL PROTECTED]>
> Subject: Re: opening JAR file from within Tomcat App
>
> Thanks for the info. Actually I'm putting some XML configuration
> files into a JAR file. They don't need to be in the classpath  per se.
> I just need a portable way to get to them.
>

If you put this JAR file inside WEB-INF/lib, it is implicitly visible to
the class loader for your webapp (there is a separate one for each webapp,
so you can't use the class path).  So, you can use the
ClassLoader.getResource() or ClassLoader.getResourceAsStream() method to
read your XML configuration files.

For example, if your config file is "com/mycompany/MyConfig.xml" in the
JAR, you would get an input stream for it (to pass to your parser) by
calling:

  InputStream is =
    this.getClass().getClassLoader().getResourceAsStream
      ("/com/mycompany/MyConfig.xml");

One really nice feature is that this call works, unchanged, if you choose
to unpack your JAR file into WEB-INF/classes instead.

Another really nice feature is that this works even if your servlet
container runs your webapp directly out of a WAR file (which it is
perfectly free to do), so there is no such thing as the absolute pathname
to the JAR.

> -Chris

Craig


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to