转自:http://www.myexception.cn/java%20exception/95.html
源码如下:
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
public class Test {
public static void main(String args[]) {
try {
File f = new File("D:/data.xml");
if (f.exists()) {
SAXReader reader = new SAXReader();
reader.setEncoding("UTF-8");
Document document = reader.read(f);
if (document == null) {
System.out.println("hehe");
}else{
System.out.println(document.content());
<b>List list = document.selectNodes("//root/author");</b>
System.out.println(list.size());
}
}else
System.out.println("file not exist");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<root name="max">
<author name="James" location="UK">James Strachan</author>
<author name="Bob" location="US">Bob McWhirter</author>
</root>
运行错误信息如下:
[org.dom4j.tree.DefaultElement@7a84e4 [Element: <root attributes:
[org.dom4j.tree.DefaultAttribute@1aaa14a [Attribute: name name value
"max"]]/>]]
java.lang.NoClassDefFoundError: org/jaxen/JaxenException
at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)
at test2.Test.main(Test.java:21)
Exception in thread "main"
错误出在List list = document.selectNodes("//root/author");这里,但我不知道为什么错
------解决方法--------------------------------------------------------
少包 jaxen.jar
------解决方法--------------------------------------------------------
同上,在dom4j的解压包lib目录下把jaxen.jar放到你的类路径里去
总结java.lang.NoClassDefFoundError可能有俩种:
1.java.lang.NoClassDefFoundError指向的"org/jaxen/JaxenException "这个字符串所指对象的类不存在
如不是自定义类的话,到 http://www.findjar.com 查找包含该信息的jar包并引入.
2.类的加载先后顺序有问题
如启动web application的时候,类的加载是有一个先后顺序的,如果应该先加载的类在后面加载,别的类在调用时找不到它,那么也会报这个错.
其他 java.lang.NoClassDefFoundError 异常处理都可这样处理.