title: Not able to read file from jar in tomcat
body: <p>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.</p>
  
  <pre><code>URL resourceURL = MyClass.class.getClassLoader().getResource("xml/xslt/master.xsl");
  File xslfile = new File(resourceURL.getPath());
  AssertUtil.assertTrue(xslfile.exists(),"xsl file not found");
  </code></pre>
  
  <p>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 <code>xslfile.exists</code> returns false. The URL correctly resolves to the location of the file inside the jar as given below</p>
  
  <pre><code>file:/&lt;MY_WEBAPP_LOCATION&gt;/MyApp/WEB-INF/lib/MyComponent.jar!/xml/xslt/master.xsl
  </code></pre>
  
  <p>where MY_WEBAPP_LOCATION corresponds to the absolute path to my tomcat servers webapp directory.</p>
  
  <p>But if I rewrite the code as below to read as inputstream, it works fine.</p>
  
  <pre><code>InputStream xslFile = MyClass.class.getClassLoader().getResourceAsStream("xml/xslt/master.xsl");
  </code></pre>
  
  <p>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 ?</p>
  
  <p><strong>EDIT</strong>: 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</p>