0

I have a template folder(with files and sub folders) under the resources folder. I need to copy the entire template folder(with all files and sub folders) to another location. I was able to do it by using below line of code:

FileUtils.copyDirectory(new file(this.getClass().getClassLoader().getResource("templates").getFile()), new File(destination));

It works fine when running from IDE, but getting FileNotFoundException when I am running the jar. I found many answers when tried to look for options and tried a few, but they did not work for me. Can someone help me if you know something that worked for you.

My template folder is structured as below inside the jar:

-com [all my classes are under this path]

-templates [I want to copy this(including all files with sub-folders to a diff location)]

-META-INF

roovs
  • 1
  • 1
    Does this answer your question? [How do I read a resource file from a Java jar file?](https://stackoverflow.com/questions/403256/how-do-i-read-a-resource-file-from-a-java-jar-file) – pringi Feb 16 '22 at 17:13
  • Please format at least your code properly, read [help/formatting](https://stackoverflow.com/help/formatting) – hc_dev Feb 16 '22 at 17:55

1 Answers1

0

Use getResourceAsStream as Stream to load the files.

getClass().getClassLoader().getResourceAsStream("fileName")

When packaged as a jar, we won't be able to read it using the getResource method as it is not in the file system. Maintain metadata for the files and use getResourceAsStream to read it.

Satz
  • 1
  • 2