0

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

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”中的异常 java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader at Main.main(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) 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 还有 1 个

如果我查看 jar 文件:

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

1061 Mon Jan 25 09:34:34 EST 2010 org/dom4j/io/SAXReader$SAXEntityResolver.class 13170 Mon Jan 25 09:34:34 EST 2010 org/dom4j/io/SAXReader.class

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

4

1 回答 1

1

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

java -cp /usr/share/java/dom4j.jar;. Main
于 2012-06-20T05:19:33.890 回答