java Source code not compiling with jdk 6.
import org.w3c.dom.Node;
Node node = list.item(0);
String txtContent = node.getTextContent();
getTextContent() not found in the jdk 6
how can solve开发者_运维问答 this compilation issue.
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.
Although a late post... maybe someone will find this useful.
I didn't like the manual project setting with JRE on top because all my colleagues would have to go trough this every time the project was imported, so I found another solution.
In my case I had the following dependency tree: org.reflections (0.9.8) -> dom4j (1.6.1) -> xml-apis (1.0.b2).
In order to fix it, I added an exclusion in the org.reflections dependency, like this:
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.8</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
In your case maybe there is another dependency that imports a faulty jar. E.g.: Dealing with "Xerces hell" in Java/Maven?
Please check http://www.jarfinder.com/index.php/java/info/org.w3c.dom.Node to see all the jars that contain that class.
I have also faced the similar problem. It is nothing to do with the Source Code. Steps to solve the problem is,
- Go to project Properties (Right click on project and choose properties)
- Select Java Build Path
- Go to Order and Export tab
- Make sure your JRE library is on TOP. If it is not in top, make it to top.
- Click on OK and see after completing the build progress in right down.
For anyone working with hibernate the solution is similar to virussu_ro above.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
Possibly a long shot, but are you sure you're using JDK 6? Sometimes IDEs like Eclipse have a dropdown in the Project window that lets you choose a version, and it may be set to 1.4. Or you may have your CLASSPATH set incorrectly. Worth checking if you have ever had Java 1.4 installed.
I ask because Node.getTextContent
was added in JDK 1.5, so the only way I can explain its absence is that you are using the Java 1.4 version of org.w3c.dom.Node.
Try running this code to determine location of jar:
System.out.println(System.getProperty("java.version"));
ProtectionDomain domain = org.w3c.dom.Node.class.getProtectionDomain();
System.out.println("domain: " + domain);
CodeSource codeSource = domain.getCodeSource();
if(codeSource != null) {
System.out.println("location: " + codeSource.getLocation());
} else {
System.out.println("null location, probably JRE class");
}
Maybe there is a jar on classpath that contains org.w3c.dom.Node class and hides the correct one.
精彩评论