A common source of confusion among (somewhat) new Java developers (especially when they are packaging their first application as a JAR) is resources on the classpath and how they are different from files.
I would request an article clarifying these things, specifically the following:
What's the difference between resources and files?
When I access a file in my source folder using java.io.File (or similar), why does it not work in a JAR deployed somewhere (in a different directory, on a different computer, ...)
Why should I not use files for accessing anything in the source folder?
What does Class#getResource/Class#getResourceAsStream do?
What's the difference between getClass.getResource("a.txt"), getClass().getResource("/a.txt") and getClass().getClassLoader().getResource("a.txt")?
Why does it not find my resource when I use the wrong one?
Where should I put resources?
with Maven/Gradle but also in plain Eclipse/IntelliJ/VSC projects
A common source of confusion among (somewhat) new Java developers (especially when they are packaging their first application as a JAR) is resources on the classpath and how they are different from files.
I would request an article clarifying these things, specifically the following:
java.io.File
(or similar), why does it not work in a JAR deployed somewhere (in a different directory, on a different computer, ...)Class#getResource
/Class#getResourceAsStream
do?getClass.getResource("a.txt")
,getClass().getResource("/a.txt")
andgetClass().getClassLoader().getResource("a.txt")
?