Figuring out path to resources in built jar

Im currently doing this to read my shader files:
Utils.readFile("src/main/resources/assets/base/shaders/scene.vert")
Utils.readFile("src/main/resources/assets/base/shaders/scene.vert")
and it works when just testing in intellij, but if i build the jar it no longer works as the src/main/resources part of the path doesnt exist. Any solutions?
4 Replies
JavaBot
JavaBotā€¢4w ago
āŒ› This post has been reserved for your question.
Hey @š“pothicon! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1stā€¢4w ago
You should access it as a resource insstead In a JAR, there is no src/main/resources You can do getClass().getClassLoader().getResource("assets/base/shaders/scene.vert")
š“pothicon
š“pothiconOPā€¢4w ago
alr im doing that now and it is getting the correct path however it gives a NoSuchFileException when i attempt to use that path to read the file can you not read files that are inside jars? i figured out a solution:
public static String readFile(String filePath) {
List<String> file = new BufferedReader(new InputStreamReader(Utils.class.getClassLoader().getResourceAsStream(filePath))).lines().toList();
StringBuilder data = new StringBuilder();
for (String s : file) {
data.append(s).append("\n");
}
return data.toString();
}
public static String readFile(String filePath) {
List<String> file = new BufferedReader(new InputStreamReader(Utils.class.getClassLoader().getResourceAsStream(filePath))).lines().toList();
StringBuilder data = new StringBuilder();
for (String s : file) {
data.append(s).append("\n");
}
return data.toString();
}
JavaBot
JavaBotā€¢4w ago
Post Closed
This post has been closed by <@465515365108154380>.
Want results from more Discord servers?
Add your server