1

I created a game in Java, and for the game I need to save gaming configurations in files, and all files are in resources/team/. In order to open, create and delete teams I get all filenames in this directory using listFiles() and "src/main/resources/team/" as directory path.

The problem is that in Intellij IDEA it works perfectly both from Main and from JAR-file, but if I try to run it in console java -jar Game.jar or open it from folder with Java, I get NullPointer from Scanner, because in directory /team/ is simply in root of JAR, without src/main.

I tried to replace it with Directory Streams and some other solutions for getting filenames, but they all use File, so I still need to write "src/main" for it to work in IDEA. Is there any possible way to change it with relative paths for /team/ in JAR or different way to get filenames using getResourceAsStream, so I could use only this with directories in resources, without listFiles()? Also I use Maven, I supposed, It's possible to solve it in pom.xml, but I couldnt find any information about it.

  • You cannot write to resources in a JAR. You need to use a directory outside the resources to store the data of your game. – J Fabian Meier Apr 28 '23 at 12:49
  • Have you tried using java.nio.file.DirectoryStream to load the files within jar as suggested in one of the below reference answers https://stackoverflow.com/questions/11012819/how-can-i-access-a-folder-inside-of-a-resource-folder-from-inside-my-jar-file – Madhawa Gunasekara Apr 28 '23 at 13:38

0 Answers0