getResource 未在 web 应用程序中加载资源
我创建了一个用于解组 XML 的 jar,并且使用以下代码来搜索 xsd
Thread.currentThread().getContextClassLoader().getResource(DEFAULT_XSD_NAME)
,其中 DEFAULT_XSD_NAME="destination.xsd"
我的 xsd 文件与具有上述代码的类位于相同的包结构中且处于同一级别。 这在我的独立应用程序中工作正常,但是当我将 jar 放在我的 Web 应用程序中的 lib 目录下时,以下代码
Thread.currentThread().getContextClassLoader().getResource(DEFAULT_XSD_NAME).getFile()
给出了空指针异常,下面是我在 tomcat 6.0.29 上运行应用程序的堆栈跟踪
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其放入
default package
中,它应该可以工作,或者
Place it in
default package
it should workor
因为,在容器中,如果您将其命名为
destination.xsd
,它会尝试从 URI 路径开始查找。将/
放在前面,例如/destination.xsd
,将使其在 servlet 容器根目录中查找。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.