Java dom4j org / jaxen / NamespaceContext异常

时间:2012-10-17 11:48:54

标签: java dom4j

我已下载dom4j-1.6.1并将其添加到java的构建路径中。我也熟悉java.lang.NoClassDefFoundError: org/saxpath/SAXPathException,但我一直有例外。

附上一个片段:

public class Parser {
    public static void parse(final String path) throws Exception {
        final SAXReader reader = new SAXReader();
        final Document document = reader.read(new File(path).toURI().toURL());
        if (document == null) return;
        List list = document.selectNodes("/");
        for (Object o : list)
            System.out.println(o);
    }
}

当我运行它时,我得到以下堆栈跟踪

Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/NamespaceContext
    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 Parser.parse(Parser.java:15)
    at Main.main(Main.java:6)
Caused by: java.lang.ClassNotFoundException: org.jaxen.NamespaceContext
    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:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 5 more

有什么原因导致错误?

2 个答案:

答案 0 :(得分:20)

例外:

java.lang.ClassNotFoundException: org.jaxen.NamespaceContext

也许您忘记在您的Java构建路径中包含jaxen.jar

有关使用SAXReader解析某些XML并在节点中循环的更具体说明: https://stackoverflow.com/a/24959790/445131

答案 1 :(得分:2)

找到解决方案。我必须下载并在java构建路径中包含jaxen