-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Bibin,

On 1/28/13 6:07 AM, Bibin Balakrishnan wrote:
> I have a web application running under tomcat 7, and in one of the
> class, Im trying to read a file in one of the jar under WEB-INF/lib
> folder.
> 
> URL resourceURL = 
> MyClass.class.getClassLoader().getResource("xml/xslt/master.xsl");

I usually see paths starting with a "/" but that doesn't seem to be a
requirement of the ClassLoader API.

> File xslfile = new File(resourceURL.getPath());

As Konstantin points out, that's not going to work unless the
resourceURL is an actual file on the disk. This URL:

> file:/<MY_WEBAPP_LOCATION>/MyApp/WEB-INF/lib/MyComponent.jar!/xml/xslt/master.xsl

...is
> 
a file in a JAR on the disk. If you want to read this file,
you're going to have to call URL.openStream to read from it, or use
ClassLoader.getResourceAsStream as you have done below:

> InputStream xslFile = 
> MyClass.class.getClassLoader().getResourceAsStream("xml/xslt/master.xsl");
>
> 
Can anyone explain what is preventing the creation of File from the jar
> resource, whereas the inputstream creation is working perfectly
> fine. Is there any additional permission settings needed from
> tomcat side, to read a file inside jar ?

Nope, the java.io.File is simply never going to work because you are
trying to read something that is not a file on the disk.

> EDIT: One more observation, if the file is placed under
> WEB-INF/classes, creation of File with above code works fine. Issue
> is only when it is placed in a jar under WEB-INF/lib

Correct. In that case, you do have a file on the disk to read... it's
no longer tucked-inside a JAR File.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEAREIAAYFAlEGmAQACgkQ9CaO5/Lv0PA5oQCdH7kfLOueStm/3YtIQ8Kte/qD
3KwAnRycJfiKuSnwgHZ+1dOk+kl7gwAv
=BdZz
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to