How can I iterate entries in a URL resource pointing to a folder in a JAR

jar, java, url

Solution

See "List files inside a JAR".

Problem

I have a situation where an app I'm writing works fine from the IDE but fails when deployed into a jar. What I see is NULL pointer exception. What I'm trying to do is get a URL resource of a directory and then iterate through the files in that directory. The URL seems to work but I can't find a way to get the files from it. So I can't seem to get a file list (because this is really a resouce list inside the jar). Any ideas? TIA ``` URL scriptFolder = getClass().getResource("/scripts/"); log.debug(scriptFolder); if (scriptFolder != null) { File folder = new File(scriptFolder.getFile()); File[] files = folder.listFiles(); // files is NULL here. for (int i = 0; i < files.length; i++) { if (files[i].isFile()) { log.debug("File " + files[i].getName()); } } } ```

Original source

Related problems