Umesh Awasthi
Umesh Awasthi

Reputation: 23587

getResource not loading resource in webapp

i have created a jar for unmarshalling the XML and i was using the following code to search an xsd

Thread.currentThread().getContextClassLoader().getResource(DEFAULT_XSD_NAME)

where DEFAULT_XSD_NAME="destination.xsd"

my xsd file is in the same package structure and at the same level where my class having the above code is. This was working fine in my stand alone application but when i placed the jar in my web-application under the lib directory the following code

Thread.currentThread().getContextClassLoader().getResource(DEFAULT_XSD_NAME).getFile()

giving me null pointer exception below is the stack trace

java.lang.NullPointerException

    com.raisonne.tr.impex.xmlprocessor.impl.XMLUnmarshallerImpl.validateXML(XMLUnmarshallerImpl.java:194)
    com.raisonne.tr.impex.xmlprocessor.impl.XMLUnmarshallerImpl.unmarshal(XMLUnmarshallerImpl.java:85)
    com.raisonne.tr.service.impex.impl.DestinationImportServiceImpl.parseXMLToObject(DestinationImportServiceImpl.java:95)
    com.raisonne.tr.service.impex.impl.DestinationImportServiceImpl.startDestinationImport(DestinationImportServiceImpl.java:82)
    com.raisonne.tr.backoffice.actions.impex.DestinationImportAction.destinationImport(DestinationImportAction.java:118)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)

i am running the application on tomcat 6.0.29.

any help/pointer is this regard is much appriciated.Additonaly it will be good if i have some solution that is container independent.

Upvotes: 2

Views: 4773

Answers (2)

Adeel Ansari
Adeel Ansari

Reputation: 39907

Because, in the container if you just name it like destination.xsd, it would try to look from the URI path onwards. Placing a / in front like, /destination.xsd, will make it look in servlet container root directory.

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240996

Place it in default package it should work

or

Thread.currentThread().getContextClassLoader().getResource("/com/your/package/"+DEFAULT_XSD_NAME)

Upvotes: 4

Related Questions