You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Bibin Balakrishnan <bi...@gmail.com> on 2013/01/28 12:07:20 UTC
Not able to read file from jar in tomcat
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");
File xslfile = new File(resourceURL.getPath());
AssertUtil.assertTrue(xslfile.exists(),"xsl file not found");
Both MyClass and master.xsl resides in the same jar and there is no issue
with packaging. But above snippet fails in the assertion statement as
xslfile.exists returns false. The URL correctly resolves to the location of
the file inside the jar as given below
file:/<MY_WEBAPP_LOCATION>/MyApp/WEB-INF/lib/MyComponent.jar!/xml/xslt/master.xsl
where MY_WEBAPP_LOCATION corresponds to the absolute path to my tomcat
servers webapp directory.
But if I rewrite the code as below to read as inputstream, it works fine.
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 ?
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
Re: Not able to read file from jar in tomcat
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Not able to read file from jar in tomcat
Posted by Konstantin Kolinko <kn...@gmail.com>.
2013/1/28 Bibin Balakrishnan <bi...@gmail.com>:
> 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");
> File xslfile = new File(resourceURL.getPath());
java.io.File represents a file on a hard drive.
A file contained inside a JAR archive cannot be represented by
java.io.File, but it can be by java.net.URL. Thus using an url works,
but using a file does not.
> AssertUtil.assertTrue(xslfile.exists(),"xsl file not found");
> Both MyClass and master.xsl resides in the same jar and there is no issue
> with packaging. But above snippet fails in the assertion statement as
> xslfile.exists returns false. The URL correctly resolves to the location of
> the file inside the jar as given below
>
> file:/<MY_WEBAPP_LOCATION>/MyApp/WEB-INF/lib/MyComponent.jar!/xml/xslt/master.xsl
> where MY_WEBAPP_LOCATION corresponds to the absolute path to my tomcat
> servers webapp directory.
>
> But if I rewrite the code as below to read as inputstream, it works fine.
>
> 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 ?
>
> 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
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org