solor启动和xpath相关抛异常的解决

上线一个基于solr的基于rest的搜索应用服务的过程中,启动tomcat,后台抛出了两个异常

 

第一个异常

 

java.lang.RuntimeException: java.lang.ExceptionInInitializerError
	at com.taobao.terminator.manage.common.IndexBuilder.initialize(IndexBuilder.java:111)
	at com.taobao.terminator.manage.common.IndexBuilder.(IndexBuilder.java:59)
	at com.taobao.terminator.pubhook.servlet.BuildIndexAction$1.run(BuildIndexAction.java:170)
	at java.lang.Thread.run(Thread.java:636)
Caused by: java.lang.ExceptionInInitializerError
	at com.taobao.terminator.manage.common.IndexBuilder.initialize(IndexBuilder.java:94)
	... 3 more
Caused by: java.lang.RuntimeException: XPathFactory#newInstance() failed to create an XPathFactory for the default object model: http://java.sun.com/jaxp/xpath/dom with the XPathFactoryConfigurationException: javax.xml.xpath.XPathFactoryConfigurationException: No XPathFctory implementation found for the object model: http://java.sun.com/jaxp/xpath/dom
	at javax.xml.xpath.XPathFactory.newInstance(Unknown Source)
	at org.apache.solr.core.Config.(Config.java:42)
	... 4 more
 

这个问题以前没有碰见过,并且在自己本地PC机上测试是不会抛出以上异常的,但是,在线上服务上就会抛出这样的异常。解决这个问题的办法是先在google上找果然找到答案,在apache solr的官方网站上找到了,http://wiki.apache.org/solr/SolrTomcat

 

Troubleshooting Errors

It's possible that you get an error related to the following:

 

SEVERE: Exception starting filter SolrRequestFilter
java.lang.NoClassDefFoundError: Could not initialize class org.apache.solr.core.SolrConfig
        at org.apache.solr.servlet.SolrDispatchFilter.init(SolrDispatchFilter.java:76)
.........
Caused by: java.lang.RuntimeException: XPathFactory#newInstance() failed to create an XPathFactory for the default object model: http://java.sun.com/jaxp/xpath/dom with the XPathFactoryConfigurationException: javax.xml.x
path.XPathFactoryConfigurationException: No XPathFctory implementation found for the object model: http://java.sun.com/jaxp/xpath/dom
        at javax.xml.xpath.XPathFactory.newInstance(Unknown Source)

This is due to your tomcat instance not having the xalan jar file in the classpath. It took me some digging to find this, and thought it might be useful for others. The location varies from distribution to distribution, but I essentially just added (via a symlink) the jar file to the shared/lib directory under the tomcat directory. 

 

作者的解释是没有在%TOMCAT_HOME%shared/lib 这个文件夹中放xalan 相关的jar包,但是,按照作者的办法,我将xalan.jar从本地拷贝到服务上,重新启动还是不解决问题。

 

之后又对本地应用程序进行调试跟踪启动代码,最后定位到了

 

javax.xml.xpath.XPathFactoryFinder  这个类的主要职责就就是为XPathFactory工厂类的工厂方法,通过参数找XPath的解析对象。

 

XPathFactoryFinder类中找解析对象的流程是这样的:

 

  1. 先组装出一个key,这个key是一个类前缀和工厂方法参数的组合,比如:XPathFactoryFinder类的 public XPathFactory newFactory(String uri)方法 传入的参数是http://java.sun.com/jaxp/xpath/dom 那会组装一个key值是“javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom”
  2. 相会从系统变量中找 System.getProperty(key) 如果你在项目启动时在命令行中添加了 -Djavax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom=XXXXXX这样的变量,那么就会用这个系统变量的值作为实现类作为方法的返回值
  3. 如果没有在系统变量中找到,那就到%java_home%jre/lib/jaxp.properties 这个配置为文件中找 javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom 这个变量,如果找到的话就用这个变量的值作为 .接口XPathFactory的实现类作为方法返回值返回。
  4. 如果在以上的过程中都没有找到  javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom 的值,那么XPathFactoryFinder会 返回一个默认对象作为返回结果。如果这个默认类在当前系统中不存在,那就会抛出异常了。

注意:以上在 jaxp.properties 这个类中的key中“:”前必须要加“\”反斜杠作为转义符,不然java 的Properties类会将“:”转义,例如在jaxp.properties中要这样写:javax.xml.xpath.XPathFactory \:http\://java.sun.com/jaxp/xpath/dom=com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl, 千万不能弄错,因为这个问题,让我折腾了好久。


第二个异常

2012-01-07 16:01:59,511 [org.apache.solr.core.Config] - ERROR - Exception during parsing file: solrconfig.xml:javax.xml.parsers.ParserConfigurationException: Feature 'http://apache.org/xml/features/xinclude' is not recognized.
    at org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.newDocumentBuilder(Unknown Source)
    at org.apache.solr.core.Config.<init>(Config.java:110)
    at org.apache.solr.core.SolrConfig.<init>(SolrConfig.java:130)
    at org.apache.solr.core.SolrConfig.<init>(SolrConfig.java:108)
    at com.taobao.terminator.manage.common.IndexBuilder.initialize(IndexBuilder.java:94)
    at com.taobao.terminator.manage.common.IndexBuilder.<init>(IndexBuilder.java:59)
    at com.taobao.terminator.pubhook.servlet.BuildIndexAction$1.run(BuildIndexAction.java:174)
    at java.lang.Thread.run(Thread.java:636)


处理这个异常只要在tomcat的启动命令JAVA_OPTS参数上,添加一个系统变量:-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl 这样就好了。





 

你可能感兴趣的:(xpath,XPathFactory)