getResource("xmlfile.xml") throws java.lang.NullPointerException -
i have xmlfile.xml
in /webcontent/web-inf
folder of web project.
i'm trying path of file using;
url url = this.getclass().getclassloader().getresource("xmlfile.xml"); string filepath = url.getfile();
but it's throwing java.lang.nullpointerexception
in second line of code.
what reason this? file available webcontent/web-inf/xmlfile.xml
you should use servletcontext
:
servletcontext context = .... // context here inputstream resourcecontent = context.getresourceasstream("/web-inf/xmlfile.xml");
or real path :
string realpath = context.getrealpath("/web-inf/xmlfile.xml");
remember, java ee webapp single (war) file, , therefore file system access components within war not guaranteed. can access objects using standard java class loader mechanism, won't give access paths of war aren't in war's class path (web-inf/classes , web-inf/lib jars). hence fails in case.
Comments
Post a Comment