tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Konstantin Kolinko <knst.koli...@gmail.com>
Subject Re: Not able to read file from jar in tomcat
Date Mon, 28 Jan 2013 12:47:15 GMT
2013/1/28 Bibin Balakrishnan <bibin.balakrishnan@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


Mime
View raw message