I know this question has been asked before but I can't find an answer that appears to be the de-facto way to do it.
I simply want to list all the resource files that are packaged in a particular folder inside my JAR file.
The JAR file contains a folder called variants and inside it are an unknown number of JSON files that I want to list.
Initially I tried this:
File[] files = new File(getClass().getClassLoader().getResource("variants").getPath()).listFiles();
This worked when running the program in Eclipse but not when running the JAR file from the command line because the path returns:
file:/dev-root/target/my-app.jar!/variants
Using this answer I was able to get it to work but it seems overly complex, it's not the accepted or even the most popular answer. Is there a straightforward way to achieve what I want?
EDIT
Using this answer I produced the following:
JarFile jar = new JarFile("target/myapp.jar");
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements())
{
JarEntry entry = entries.nextElement();
if (!entry.isDirectory() && entry.getName().contains("variants"))
{
// add name to files []
}
}
Is it possible to get a list of all the files in one go using this technique?
E.g. using something like listFiles
.