I need to read a properties files that's buried in my package structure in com.al.common.email.templates
.
I've tried everything and I can't figure it out.
In the end, my code will be running in a servlet container, but I don't want to depend on the container for anything. I write JUnit test cases and it needs to work in both.
Nobody mentions the similar but even simpler solution than above with no need to deal with the package of the class. Assuming myfile.properties is in the classpath.
Enjoy
To add to Joachim Sauer's answer, if you ever need to do this in a static context, you can do something like the following:
(Exception handling elided, as before.)
When loading the Properties from a Class in the package
com.al.common.email.templates
you can use(Add all the necessary exception handling).
If your class is not in that package, you need to aquire the InputStream slightly differently:
Relative paths (those without a leading '/') in
getResource()
/getResourceAsStream()
mean that the resource will be searched relative to the directory which represents the package the class is in.Using
java.lang.String.class.getResource("foo.txt")
would search for the (inexistent) file/java/lang/String/foo.txt
on the classpath.Using an absolute path (one that starts with '/') means that the current package is ignored.
I managed to solve this issue with this call
Extra, you have to put your whatever.properties file in /src/main/resources
Assuming your using the Properties class, via its load method, and I guess you are using the ClassLoader getResourceAsStream to get the input stream.
How are you passing in the name, it seems it should be in this form:
/com/al/common/email/templates/foo.properties