【发布时间】:2017-08-12 10:00:20
【问题描述】:
概述:去年,我监督了一个将 Web 应用程序从 WebSphere 5/Java 4 移植到 Tomcat 7/Java 7 的承包商。今年,我们发现一个很少使用的功能被破坏了。组织变化使在该领域工作的团队只剩下我一个人,所以我没有人可以讨论这个问题。
我将问题追踪到 getClass().getResource("/My_Form.xsd") 返回 null。我检查了很明显:war 文件的根目录确实包含 My_Form.xsd,它位于 Tomcat 爆炸 war 文件的磁盘上。
这是代码(从原始 Java 4 版本略微重构):
package com.mycompany;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
...
public class MyFormMaintenanceFactory {
...
public DocumentBuilderFactory getDocumentBuilderFactory()
throws Exception
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
dbf.setIgnoringComments(true);
dbf.setIgnoringElementContentWhitespace(true);
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
String schema = "/My_Form.xsd";
URL schemaUrl = getClass().getClassLoader().getResource( schema);
if (schemaUrl == null) {
throw new Exception("Could not getResource " + schema);
}
dbf.setAttribute( JAXP_SCHEMA_SOURCE, schemaUrl.toExternalForm());
return dbf;
}
...
}
输出是来自“throw new Exception(...)”行的堆栈跟踪:
17:38:56.756(03/20) INFO MyApp : java.lang.Exception: Could not getResource /My_Form.xsd
还有一个堆栈跟踪。
我尝试过其他形式的通话。这些都没有更好的效果:
URL schemaUrl = getClass().getResource("/My_Form.xsd");
URL schemaUrl = DocumentBuilderFactory.class.getClassLoader().getResource( schema);
针对另一个关于 getResource() 返回 null 的问题,建议是运行 jar tf AppName.war。
我这样做了,My_Form.xsd 位于 war 文件的根目录:
$ jar tf MyApp-3.0.1-SNAPSHOT.war
META-INF/
META-INF/MANIFEST.MF
Global/
...
Modules/
...
shared/
theme/
WEB-INF/
WEB-INF/classes/
WEB-INF/classes/com/
...
WEB-INF/classes/resources/
WEB-INF/lib/
WEB-INF/lib/tomcat_jars/
...
WEB-INF/struts-config/
WEB-INF/taglibs/
Global/css/components/basicControl.css
...
Global/image/arrow.gif
Global/image/atsign.jpg
Global/image/banner_background.gif
...
Global/JavaScript/components/ajax.js
,,,
Global/jsp_pages/components/basicControl.jsp
Global/jsp_pages/components/basicControlTitle.jsp
...
index.jsp
...
Modules/Global_Internal/jsp_pages/body.jsp
Modules/Global_Internal/jsp_pages/layout.jsp
Modules/Global_Internal/jsp_pages/sidebar.jsp
Modules/help/FAQ.html
...
shared/searchUtil.js
...
showAllTickets.jsp
showAllTickets_body.jsp
My_Form.xsd
theme/Master.css
WEB-INF/classes/com/mycompany/MyFormMaintenanceFactory.class
WEB-INF/classes/com/mycompany/MyFormMaintenanceFactory.java
...
WEB-INF/classes/resources/MyApp.properties
...
WEB-INF/lib/commons-beanutils-1.7.0.jar
WEB-INF/lib/commons-beanutils.jar
WEB-INF/lib/commons-chain-1.2.jar
WEB-INF/lib/commons-collections-2.1.jar
...
WEB-INF/web.xml
META-INF/maven/
...
$
我已经阅读了文档以及我能找到的关于其他人出现此错误的每一页,但我无法弄清楚代码做错了什么。同样的调用在 WebSphere 5/Java 4 中有效。我将不胜感激地接受任何和所有建议......
【问题讨论】:
-
尝试将其移至
WEB-INF/classes/resources/
标签: java