listFiles() method in Java doesn't work in .jar

Matt :

I have a "Pictures" folder in my project like this: link

I want to find the number of files in some of these folders.

I'm using: int length = new File("./Pictures/1").listFiles().length which works perfectly fine when I run the IDE. However when I convert my project to .jar File I get a NullPointerException.

Kayaman :

A jar file is basically a zip file. Your code would be looking for the Pictures directory on your file system, not inside the jar file. To access files inside a jar file (also works outside of jar, it searches from the classpath) you can use Class.getResource() (and getResourceAsStream()).

However I'm not certain whether it's possible to use those for file listings as you want, they're mostly used for retrieving single resources from the classpath.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=116688&siteId=1