gpt4 book ai didi

java - 线程 "main"java.lang.NoClassDefFoundError : org/dom4j/io/SAXReader 中出现异常

转载 作者:行者123 更新时间:2023-12-01 15:19:46 27 4
gpt4 key购买 nike

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

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

Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/io/SAXReader at Main.main(Main.java:31) Caused by: 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 java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 1 more

如果我查看 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 中存在“缺失”的类。关于如何解决这个问题有什么想法吗?

最佳答案

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

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

关于java - 线程 "main"java.lang.NoClassDefFoundError : org/dom4j/io/SAXReader 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11113210/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com