Search code examples
web-applicationsjarclasspathclassloaderweb-inf

Load resource file outside jar


I have created a jar which contains a class that loads an external resource file from class path of my web application. My application is running on resin server. I have deployed my jar in web-inf/lib and my resource file is in web-inf/classes folder. Now the resource file is being accessed using

getClass().getResourceAsStream("/resource-name.json");

However, the application throws a null pointer exception when I run it. Could anyone explain to me why my class loader is not able to find this resource file located in web-inf/classes folder? I searched this forum but did not find anything like my problem here.

Like always, I appreciate your responses and thank you.


Solution

  • To investigate, first try to print all locations where your ClassLoader tries to load classes/resources:

    ((URLClassLoader) getClass().getClassLoader()).getURLs();
    

    assuming that the Resign's ClassLoader extends URLClassLoader. It should contain your folder.