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/07/29 21:18:50 UTC

Re[2]: opening JAR file from within Tomcat App

Hello Chris,

put them in WEB-INF and do:

InputStream is =
    getServletContext().getResourceAsStream("/WEB-INF/myConfig.xml");


That is guaranteed to work whether your app is served off the
filesystem (in a directory) or directly from a .war file.

Alternatively, you can do:

InputStream is =
Thread.currentThread().getContextClassLoader().getResourceAsStream("myConfig.xml");

That will find your xml file anywhere in your class path including all
of Tomcat's special classloaders (other than the server classloader
since that is hidden from webapps).

Either way is totally portable with the latter way being usable in
normal Java applications.  I'd use the former in servlet code since it
is guaranteed to only be run in the context of a running servlet
container and the latter for general application code that could be
run both under a servlet container or on its own in a normal app.

Either way, there is no need to have direct access to a specific .jar
file unless you want to modify the files at runtime and store them
back into the .jar file.  However, I would not recommend this anyway
since the changes can get thrown away just by deleting the webapp.

Jake


Monday, July 29, 2002, 12:30:09 PM, you wrote:

CR> Thanks for the info. Actually I'm putting some XML configuration
CR> files into a JAR file. They don't need to be in the classpath  per se.
CR> I just need a portable way to get to them.

CR> -Chris


CR> ----- Original Message -----
CR> From: "Shapira, Yoav" <Yo...@mpi.com>
CR> To: "Tomcat Users List" <to...@jakarta.apache.org>
CR> Sent: Monday, July 29, 2002 1:19 PM
CR> Subject: RE: opening JAR file from within Tomcat App


CR> Hi,
CR> It's somewhat unusual to read a JAR file (containing only compiled
CR> .class files?) at runtime.  Are you doing some sort of fancy reflection
CR> work?

CR> Anyways, to your question.  Do you need the file to be on the classpath,
CR> i.e. its classes visible to the classloader?  Or do you only need to
CR> open it up yourself?

CR> If you only need to look inside it yourself, put in under
CR> /MyApp/myjar.jar.  Then you get do stuff like
CR> ServletContext.getResource("/myjar.jar") or
CR> ServletContext.getRealPath("/myjar.jar").

CR> Yoav Shapira
CR> Millennium ChemInformatics


>>-----Original Message-----
>>From: Chris Ruegger [mailto:cruegger@speakeasy.net]
>>Sent: Monday, July 29, 2002 1:21 PM
>>To: Tomcat Users List
>>Subject: opening JAR file from within Tomcat App
>>
>>
>>I need to open a JAR file from within my Tomcat app and read some of
>>the files. Question: What's the best way to get the absolute path Name
>>of the Jar file so that I can pass that name to a JarFile object?
>>
>>I tried placing the JAR file in the WEB-INF\lib directory thinking its
CR> full
>>path name would show up when I performed:
>>System.getProperty("java.class.path").  However, this call returns only
>>the JDK tools.jar file and the tomcat bootstrap.jar file, even though
>>I have many JAR files in my WEB-INF\lib.
>>
>>What approaches have other folks used for this?
>>Thanks


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






CR> --
CR> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
CR> 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>