找不到http://www.w3.org/2001/XMLSchema的工厂 [英] Unable to find a factory for http://www.w3.org/2001/XMLSchema
问题描述
我的以下代码有一些烦人的问题,在切换到Java 1.7之前一直可以正常工作
I'm having some annoying problems with the following code, that worked okay until switched to Java 1.7
import javax.xml.validation.SchemaFactory;
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
使用-Djaxp.debug = 1从Netbeans运行,会引发以下错误:
Running from Netbeans with -Djaxp.debug=1, the following error is thrown:
上面的代码片段是OSGI软件包的一部分
The above snipped code is part of an OSGI bundle
JAXP: using thread context class loader (sun.misc.Launcher$AppClassLoader@5e3a78ad) for search
JAXP: Looking up system property 'javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema'
JAXP: The property is undefined.
JAXP: found null in $java.home/jaxp.properties
JAXP: no META-INF/services/javax.xml.validation.SchemaFactory file was found
JAXP: attempting to use the platform default XML Schema validator
JAXP: instanciating org.apache.xerces.jaxp.validation.XMLSchemaFactory
JAXP: failed to instanciate org.apache.xerces.jaxp.validation.XMLSchemaFactory
java.lang.ClassNotFoundException: org.apache.xerces.jaxp.validation.XMLSchemaFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at javax.xml.validation.SchemaFactoryFinder.createInstance(Unknown Source)
at javax.xml.validation.SchemaFactoryFinder._newFactory(Unknown Source)
at javax.xml.validation.SchemaFactoryFinder.newFactory(Unknown Source)
at javax.xml.validation.SchemaFactory.newInstance(Unknown Source)
JAXP: unable to find a factory for http://www.w3.org/2001/XMLSchema
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema
我还制作了一个只有工厂实例的小型Java应用程序
I have also made a small Java app with only the factory instance
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
工厂是"com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory" 的实例,而在我的捆绑包中,它试图实例化"org.apache. xerces.jaxp.validation.XMLSchemaFactory" (可能无法找到它).
The factory is instance of "com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory" while in my bundle it tries to instantiate "org.apache.xerces.jaxp.validation.XMLSchemaFactory" (and probably fails finding it).
为什么会有这种区别? 似乎是什么问题,有什么想法吗?
Why this difference ? What seems to be the problem, any ideea ?
推荐答案
我通过使用线程上下文类加载器(通常缩写为TCCL)来使其工作.这是代码:
I got this to work by using the Thread Context Class Loader (often abbreviated TCCL). Here's the code:
ClassLoader original = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(ClassWithinYourBundle.class.getClassLoader());
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
} finally {
Thread.currentThread().setContextClassLoader(original);
}
适当替换ClassWithinYourBundle
.
这里是 GitHub项目.
有关TCCL的更多信息,请参见: https://articles.qos.ch/classloader.html
For more information on TCCL, see: https://articles.qos.ch/classloader.html
这篇关于找不到http://www.w3.org/2001/XMLSchema的工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!