今天在更新项目后进行编译时,出现如下错误一堆:
Google之,在stackoverflow
上看到如下的解决方法:
I came here with the same problem. Even worse: I had two projects side by side, both targetting the same JRE (1.6), and one was able to resolve Node.getTextContent() while the other wasn't. I resolved it sort of by accident; I went to project properties | Java Build Path | Order and Export tab, selected the JRE (which was at the bottom of the list) and clicked the "Top" button to move it to the top. My problem went away. It appears that the Node I wanted was hidden by another one. :- Maybe this will help with your problem.
大体解决方法就是:
在项目的Java Build Path | Order and Export
选项卡中,将JRE System Library
选中,并Top
置顶。然后再进行编译即可。如图:
但是上面并没有给出原因。
其实顺着问题的解决思路想想,肯定是jar出现了冲突所致。于是我就在项目的jar包中找可能含有org.w3c.dom.Element
这个类的jar包。既然将JRE的lib进行了置顶,那么就有理由猜测JRE-lib里存在这个类的相关方法。
最终,在rt.jar
和xml-apis.jar
和中找到了。应该就是这两个jar冲突所致,由于引用优先级的不同导致引用了xml-apis.jar
中的方法。
其实在pom.xml
中并没有这个jar的直接引用,在Dependency Hierarchy
视图中搜索xml-apis
可以发现,它其实是由于dom4j
的依赖而引入的。如图:
解决方法:右击该jar,选择exclude maven artifact
,确认并保存,重新编译即可:
最终的pom.xml
中只是在dom4j
的<dependency>
中多了这么一段<exclusions>
:
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
<exclusions>
<exclusion>
<artifactId>xml-apis</artifactId>
<groupId>xml-apis</groupId>
</exclusion>
</exclusions>
</dependency>
参考:
http://stackoverflow.com/questions/5534864/compilation-error-in-node-gettextcontent-for-jdk-6
http://www.educity.cn/wenda/364108.html