问题
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);
Running from Netbeans with -Djaxp.debug=1, the following error is thrown:
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
I have also made a small Java app with only the factory instance
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
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 ?
回答1:
This work for me on JRE 7:
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new SAXSource(new InputSource(new ByteArrayInputStream(xsd.getBytes()))));
Validator validator = schema.newValidator();
validator.validate(new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes()))));
System.out.println("Validated !");
回答2:
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);
}
Replacing ClassWithinYourBundle
appropriately.
Here's a GitHub Project for further details.
For more information on TCCL, see: https://articles.qos.ch/classloader.html
来源:https://stackoverflow.com/questions/23222413/unable-to-find-a-factory-for-http-www-w3-org-2001-xmlschema