0

How can I get a list of files inside the resources folder in Java?

enter image description here

This is not a resources folder from test folder, also my .jar file doesn't contain the resources folder so I don't think ZipInputStream is a solution? (I'm using maven to build my jar)

I tried Enumeration<JarEntry> entries = jarFile.entries(); to list all the file, but how can I efficiently filter out entries that's not from the resources folder? (such as class file or folder contains class files...)

Thank you!

ABC
  • 145
  • 2
  • 7
  • 1
    What do you want to achieve? The resources folder of your project is a static folder. The content does not change. So you know exactly the content of the resources folder at the time you are developing your application. – McPringle Sep 12 '21 at 15:51
  • How are you deploying this? If the folder isn't in your `.jar`, then it won't be there when someone else runs your code. Are you planning to deliver the `resources` folder separately with your software? – Silvio Mayolo Sep 12 '21 at 15:54
  • “my .jar file doesn't contain the resources folder” Are you expecting this to run on any computer other than your own? It seems to me that if your resources aren’t part of your .jar file, you’ll need to distribute the resource directory along with the .jar file. – VGR Sep 12 '21 at 16:58
  • @VGR Sorry, English is not my native language. Files inside the `resources` folder in the project are in the `.jar`, but not inside a folder called `resources`, [here is the image](https://i.imgur.com/62u50WU.png). My goal is to get a list of files that is from the `resources` folder. – ABC Sep 12 '21 at 17:30
  • 1
    The short answer is: you can’t. The longer answer is: there are ways that will work, but they are not reliable. See https://stackoverflow.com/questions/56188427/how-to-copy-folders-out-of-resources-from-jar-both-in-runtime-and-dev-env. The full answer is: do as McPringle suggested and keep a plain text file as an additional resource, which contains a list of your other resources. It’s your application, so you know exactly which resources are in it. – VGR Sep 12 '21 at 20:02
  • You can write a small Java application to create that file list using File.listFiles and scanning the content of your resource folder. Run this as a test case during your build, so it is always up to date. – Christopher Sep 12 '21 at 21:27

0 Answers0