0

We have an application deployed in JBoss as WAR file. It is showing error while we are reading files using classloader as well as resource utils. If we are giving the full path hardcoded, we are able to read file. The same code is working fine with Tomcat server which is build in in spring tool. We have kept all our files to be read under resources folder.

File file = ResourceUtils.getFile(String.format("classpath:jsons/%s", filename));

Even if we are keeping files under root directory also it is not working. How can I solve the problem?Please Help.. Thankyou...

  • What is ResourceUtils and how what does it do in getFile? – Simon Martinelli Apr 12 '21 at 08:25
  • 3
    You cannot read a `File` from within a war/jar file. A `File` is a physical resource on the filesystem, that stops to be the case when that is put inside a war/jar. You can only use a `InputStream` to read the content. It works from your IDE because an exploded war/jar is used on that Tomcat instance (i.e. it is a physical file in that way). – M. Deinum Apr 12 '21 at 08:32
  • 1
    Side note: there is no benefit in using `String.format("classpath:jsons/%s", filename)` instead of `"classpath:jsons/" + filename`. – Holger Apr 12 '21 at 08:50

0 Answers0