getResource 未在 web 应用程序中加载资源

发布于 2024-10-10 03:27:32 字数 1409 浏览 0 评论 0原文

我创建了一个用于解组 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不咬妳我踢妳 2024-10-17 03:27:32

将其放入 default package 中,它应该可以工作

,或者

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

Place it in default package it should work

or

Thread.currentThread().getContextClassLoader().getResource("/com/your/package/"+DEFAULT_XSD_NAME)
我的影子我的梦 2024-10-17 03:27:32

因为,在容器中,如果您将其命名为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文