2

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.

Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204

2 Answers2

4

Place it in default package it should work

or

Thread.currentThread().getContextClassLoader().getResource("/com/your/package/"+DEFAULT_XSD_NAME)
jmj
  • 237,923
  • 42
  • 401
  • 438
  • You mean i should move out the XSD from the jar and place it some where in the package where i am using the functionality of jar?? – Umesh Awasthi Jan 05 '11 at 10:44
  • I hope your above code and xsd both are in the same jar then follow the answer – jmj Jan 05 '11 at 10:46
  • Yes Code as well the XSD are at same jar at same directory structure.and placing the package name as mentioned by you is still not clear to me..is it something related to web-application container behaviour since standalone version of the application(converted to jar) was working perfectly fine – Umesh Awasthi Jan 05 '11 at 10:53
  • `Thread.currentThread().getContextClassLoader().getResource("/com/your/package/"+DEFAULT_XSD_NAME)` **will work** for you. and yes when bundle gets extracted it will follow this way – jmj Jan 05 '11 at 10:56
  • @org.life.java:It worked but its better to ask what exactly this mean rather than following it.so can you throw some light what exactly was i doing wrong it will help me to avoid future mistakes :) – Umesh Awasthi Jan 05 '11 at 11:06
  • Sure, when you `Thread.currentThread().getContextClassLoader().getResource(DEFAULT_XSD_NAME)` it will look on the base of resources for the particular class from which you are executing this, and hence there is no `DEFAULT_XSD_NAME` named resource available on the base but there is in `/your/package`so it will resolve it – jmj Jan 05 '11 at 11:09
  • also see this explanation http://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream – jmj Jan 05 '11 at 11:12
  • @umesh: Jigar's solution is correct and indeed working. For the question why your version was not working? See my answer. BTW, +1 to Jigar. – Adeel Ansari Jan 06 '11 at 02:45
2

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.

Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
  • Thanks Adeel :) I hope my comments answers the explanation well , let me know if there is anything missing thanks btw +1 :) – jmj Jan 06 '11 at 06:37