spring boot resources not recognized

hi, i'm using spring boot v3.4.5 in IntelliJ ultimate with a very basic gradle buildscript [1] (no custom paths, no modules, just dependencies and a library as subproject) now, when running the project using gradle task bootRun, no resources are ever available, neither by intellij's builtin spring application run configuration a google search only results in the lie that it should load from src/main/resources path, but it simply does not i can see the resource files in build/resources and i have tried clean before bootRun as noted by someone on stackoverflow [2], but that also yielded no change whatsoever i just wish gradle would work as expected just once 😭 i do not want to build a jar, nor a war or anything like that; i would like to understand why bootRun and intellij's configuration both will not recognize my resources [1] https://github.com/burdoto/zitrusfalter/blob/main/build.gradle [2] https://stackoverflow.com/questions/25596783/resource-injection-in-spring-boot-not-working-with-gradle
17 Replies
JavaBot
JavaBot•7mo ago
āŒ› This post has been reserved for your question.
Hey @Kaley šŸ¦! 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•7mo ago
How do you see these resources not being recognized? Where are they? How are you accessing them?
Kaley šŸ¦
Kaley šŸ¦OP•7mo ago
getResourceAsStream("assets/background.png") always returns null
No description
dan1st
dan1st•7mo ago
Can you show the full code?
Kaley šŸ¦
Kaley šŸ¦OP•7mo ago
that method is being called on Program.class.getResourceAsStream("assets/background.png")
try (var resource = ZitrusfalterApplication.class.getResourceAsStream("assets/background.png")) {
if (resource == null) throw new IllegalStateException("Background resource not found");
img = ImageIO.read(resource);
} catch (Exception e) {
throw new Command.Error("Hintergrund kann nicht geladen werden", e);
}
try (var resource = ZitrusfalterApplication.class.getResourceAsStream("assets/background.png")) {
if (resource == null) throw new IllegalStateException("Background resource not found");
img = ImageIO.read(resource);
} catch (Exception e) {
throw new Command.Error("Hintergrund kann nicht geladen werden", e);
}
dan1st
dan1st•7mo ago
can you change it to getResourceAsStream("/assets/background.png")? ok yes
Kaley šŸ¦
Kaley šŸ¦OP•7mo ago
are you fr telling me 😐
dan1st
dan1st•7mo ago
that or ....class.getClassLoader().GetResourceAsStream("assets/background.png")
Kaley šŸ¦
Kaley šŸ¦OP•7mo ago
leading slash was the fix, lmfao ffs
dan1st
dan1st•7mo ago
if you are looking up resources using that method on a Class, they are resolved relative to the class
Kaley šŸ¦
Kaley šŸ¦OP•7mo ago
thank you very much, the kind of stupid issue
JavaBot
JavaBot•7mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
dan1st
dan1st•7mo ago
with the classloader or a leading slash, it uses the classpath root
Kaley šŸ¦
Kaley šŸ¦OP•7mo ago
so just to make sure i got that right if the resource would lie at src/main/resources/de/kaleidox/zitrusfalter/assets/background.png that first call would have worked? presuming that the package of that class is de.kaleidox.zitrusfalter
dan1st
dan1st•7mo ago
if that's the package of your main class, yes yes
Kaley šŸ¦
Kaley šŸ¦OP•7mo ago
gee, alright okay i didnt know that i work too little with resources lmao thank you very much!
JavaBot
JavaBot•7mo ago
Post Closed
This post has been closed by <@141476933849448448>.

Did you find this page helpful?