线程" main"中的例外情况java.lang.NoClassDefFoundError:org / dom4j / io / SAXReader

时间:2012-06-20 05:15:59

标签: java dom4j

我正在尝试编译并运行以下类:

import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.Element;

import java.net.URL;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.List;

/**
 * Java client program to demonstrate how to access Hudson remote API.
 *
 * @author Kohsuke Kawaguchi
 * @see http://hudson.gotdns.com/wiki/display/HUDSON/Remote+access+API
 */
public class Main {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws Exception {
        // every Hudson model object exposes the .../api/xml, but in this example
        // we'll just take the root object as an example
        URL url = new URL("http://deadlock.netbeans.org/hudson/api/xml");

        // if you are calling security-enabled Hudson and
        // need to invoke operations and APIs that are protected,
        // consult the 'SecuredMain" class
        // in this package for an example using HttpClient.

        // read it into DOM.
        Document dom = new SAXReader().read(url);

        // scan through the job list and print its status
        for( Element job : (List<Element>)dom.getRootElement().elements("job")) {
        //for( Element job : (java.util.List<org.dom4j.Element>)dom.getRootElement().elements("job")) {
            System.out.println(String.format("Name:%s\tStatus:%s",
                job.elementText("name"), job.elementText("color")));
        }
    }
}

我使用以下方法编译它:

javac -cp /usr/share/java/dom4j.jar Main.java 

但是当我试图运行它时,我得到一个例外:

  

$ java Main

     

线程“main”中的异常java.lang.NoClassDefFoundError:   Main.main中的org / dom4j / io / SAXReader(Main.java:31)引起:   java.lang.ClassNotFoundException:org.dom4j.io.SAXReader at   java.net.URLClassLoader $ 1.run(URLClassLoader.java:202)at   java.security.AccessController.doPrivileged(Native Method)at   java.net.URLClassLoader.findClass(URLClassLoader.java:190)at   java.lang.ClassLoader.loadClass(ClassLoader.java:306)at   sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301)at at   java.lang.ClassLoader.loadClass(ClassLoader.java:247)... 1更多

如果我查看jar文件:

  

jar tvf /usr/share/java/dom4j.jar | grep SAXReader *

     

1061 Mon 1月25日09:34:34 EST 2010   org / dom4j / io / SAXReader $ SAXEntityResolver.class 13170 1月25日星期一   美国东部时间09:34:34 org / dom4j / io / SAXReader.class

JAR中存在“缺失”类。有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:1)

在运行时你需要使用相同的类路径

java -cp /usr/share/java/dom4j.jar;. Main