How do I read a resource file from a Java jar file?
Looks like you want to use java.lang.Class.getResourceAsStream(String)
, see
https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResourceAsStream-java.lang.String-
It looks as if you are using the URL.toString
result as the argument to the FileReader
constructor. URL.toString
is a bit broken, and instead you should generally use url.toURI().toString()
. In any case, the string is not a file path.
Instead, you should either:
- Pass the
URL
toServicesLoader
and let it callopenStream
or similar. - Use
Class.getResourceAsStream
and just pass the stream over, possibly inside anInputSource
. (Remember to check for nulls as the API is a bit messy.)
You don't say if this is a desktop or web app. I would use the getResourceAsStream()
method from an appropriate ClassLoader if it's a desktop or the Context if it's a web app.