tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bibin Balakrishnan <bibin.balakrish...@gmail.com>
Subject Not able to read file from jar in tomcat
Date Mon, 28 Jan 2013 11:07:20 GMT
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

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message