You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Steph Richardson <st...@kvasar.com> on 2003/10/13 23:28:10 UTC

Can I read file resources from a .WAR file at runtime ?

I have a web-app that uses several xml, xsd, and other configuration files at runtime ( including during the Application Init
event ). We keep these files under various directories under WEB-INF so that we can get their paths at runtime relative to
ServletContext.getRealPath( "WEB-INF" ).

When I am deploying this web-app as a war file, I can detect I am a war file at runtime because ( getRealPath( "" ) == null ), but
is there a consistent way to read a file from this war file ?

Thanks

--------------------------------------------------------
Steph Richardson
Kvasar Technology LLC
HQ: suite 106, 822 Boylston st., Chestnut Hill, MA 02467
smtp : steph@kvasar.com
http : www.kvasar.com
--------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Can I read file resources from a .WAR file at runtime ?

Posted by Bill Barker <wb...@wilshire.com>.
Justin's suggestion works, but personally I prefer using:
  InputStream is =
getServletContext().getResourceAsStream("/WEB-INF/myresource");

"Steph Richardson" <st...@kvasar.com> wrote in message
news:CCEJLCOJKFBHFPKFNNABEEHKDDAA.steph@kvasar.com...
>
> I have a web-app that uses several xml, xsd, and other configuration files
at runtime ( including during the Application Init
> event ). We keep these files under various directories under WEB-INF so
that we can get their paths at runtime relative to
> ServletContext.getRealPath( "WEB-INF" ).
>
> When I am deploying this web-app as a war file, I can detect I am a war
file at runtime because ( getRealPath( "" ) == null ), but
> is there a consistent way to read a file from this war file ?
>
> Thanks
>
> --------------------------------------------------------
> Steph Richardson
> Kvasar Technology LLC
> HQ: suite 106, 822 Boylston st., Chestnut Hill, MA 02467
> smtp : steph@kvasar.com
> http : www.kvasar.com
> --------------------------------------------------------




---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Can I read file resources from a .WAR file at runtime ?

Posted by Justin Ruthenbeck <ju...@nextengine.com>.
Instead of referencing the files through classes in the the java.io 
packages, read the files as resources through the Classloader.  This is a 
*much* preferable way to do what you asking about -- one very good reason 
for doing it through the Classloader is the dilema you're facing now.

So to answer your question: No, there is no way to read file resources from 
an application deployed as a war file.  The solution is to read files as 
resources loaded by the Classloader.

To get you started, see:
http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg63518.html

justin


At 02:28 PM 10/13/2003, you wrote:

>I have a web-app that uses several xml, xsd, and other configuration files 
>at runtime ( including during the Application Init
>event ). We keep these files under various directories under WEB-INF so 
>that we can get their paths at runtime relative to
>ServletContext.getRealPath( "WEB-INF" ).
>
>When I am deploying this web-app as a war file, I can detect I am a war 
>file at runtime because ( getRealPath( "" ) == null ), but
>is there a consistent way to read a file from this war file ?
>
>Thanks
>
>--------------------------------------------------------
>Steph Richardson
>Kvasar Technology LLC
>HQ: suite 106, 822 Boylston st., Chestnut Hill, MA 02467
>smtp : steph@kvasar.com
>http : www.kvasar.com
>--------------------------------------------------------
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


____________________________________
Justin Ruthenbeck
Software Engineer, NextEngine Inc.
justinr - AT - nextengine DOT com
Confidential
    See http://www.nextengine.com/confidentiality.php
____________________________________


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org