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 ie 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
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.