ClassName.class.getResourceAsStream returning Null

1.1k Views Asked by At

I migrated project from Eclipse to Android Studio. App compiles fine, but it has a crash related to nekohtml library.

Inside HTMLEntities class

//filename = "res/HTMLlat1.properties"
final InputStream stream = HTMLEntities.class.getResourceAsStream(filename);

stream is always null. I tried to move file to the same folder as class and gave full path like this

filename = "/org/cyberneko/html/res/HTMLlat1.properties"

Any ideas?

1

There are 1 best solutions below

17
On

You should use filename = "/org/cyberneko/html/res/HTMLlat1.properties" instead of filename = "/org.cyberneko.html/res/HTMLlat1.properties" or use relative paths. This could be explained in this way: jar (jar is an example, maybe you're running your code from *.class in some directory) is just some kind of file system with it's root ("/") and all the files in the packages are in some subdirectories and you should specify the paths for them.