Things to consider.
You should not be using hard coded values, when using File
. Once the application is "jarred", the file system location will no longer the the same as when running from your IDE.
If you want to read a jar resource, you should read it a resource via an URL, with getClass().getResource("...")
or one of its variants.
Also it looks like (with the path you are using "resources/background"), that the "resource" is on the same level as the src
, and is not event being included to the build path of the jar. If you extract the jar, I'm pretty sure the files won't be there.
You should instead put the "resources" inside the "src", so it's automatically put in the build path. You could leave it where it is, and configure the build to add it, but that will take a longer explanation. So just put it in the "src". So you will have a file structure like
ProjectRoot
src
resources
beackground
Still the problem is trying to read the path a File
, which will search for the file using on the file system, using the hard code relative path. That won't work. As I said it should be read from an URL. So you could do something like:
URL url = TestFiles.class.getResource("/resources/background");
URI uri = url.toURI();
File file = new File(uri);
File[] files = file.listFiles();
for (File f : files) {
System.out.println(f.getAbsolutePath());
}
This should work. So just make sure after you build, that the files actually exist in the jar, then get the url of the dir you want, then you can create a File
from the URI
- See the
Class
API for more resource obtaining methods, and some explanation.