0

So my program is finally done in it's first version. I can run it in Eclipse IDE without any problems, but as soon as I pack it up into a Jar-File, it can't find the files that were in a folder within the project (declared as source folder). I already know, that while compiling, the folder structure changes and I tried working around that, but it doesn't work and I don't want to stick to such a work-around.
Project structure:

- src/main/java/
    + [my packages]
- resources/
     + configs/[some files]
     + languages/[some language files]
     + levelcards/[some images]

In my program im accessing i.e. the files inside "configs" with

new File("./resources/configs/[...]");

and it works absolutely fine, as the folder exists in the project. After compiling it doesn't though, so I tried it with

new File("./configs/[...]");

but it doesn't work either.
What I expect is it working regardless of me launching it in Eclipse or via the JRE directly, though I have no idea how to get it to that point. If you can help me out in any way, I would appreciate it!
Gregor

1 Answers1

0

eg:

Assume have a Utility class which will read from file and return as String

class Utils{

public static String readFromFile(String filePath){
  InputStream inputStream=Utils.class.getResourceAsStream(filePath); //eg:- configs/temp_file.txt
/* Now once you get the InputStream there 
are lot of utility library to return as string */

String newLine = System.getProperty("line.separator");
 BufferedReader reader = new BufferedReader(
         new InputStreamReader(inputStream));
 StringBuilder result = new StringBuilder();
 for (String line; (line = reader.readLine()) != null; ) {
     if (result.length() > 0) {
         result.append(newLine);
     }
     result.append(line);
 }
 return result.toString();
}
}
S Manikandan
  • 148
  • 8
  • Yes! I understand your solution, but I'm not reading certain lines, but I'm using the "Properties" class. Depending on the case, I'm also using the "Properties" class to write to those files. Is there any way you could adapt your solution to this? Edit: I already changed my project structure for it to work with "Class.getResource()", but now I'm getting a FileNotFound-Error, so this case is still open. I uploaded the class on pastebin, so you can have a look into it: https://pastebin.com/aGFPhjnB – Gregor7008 Dec 26 '21 at 13:44