I need to take a look at the names of files inside a specific package. Currently, I'm doing the following:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL packageUrl = loader.getResource(getPackage().replace('.', '/'));
File packageDir = new File(packageUrl.getFile());
// Work with the files inside the directory
This actually works just fine in Eclipse, since it doesn't package my application by default. When I run it from a jar file, however, I get a NullPointerException
on packageUrl
.
So, how do I obtain a list of the contents of a package that's inside a jar? My research suggested using getResourceAsStream
, but I'm not quite sure how to explore a directory with an InputStream
, if that's even possible.