2012-06-20 82 views
0

我試圖編譯和運行以下類:異常在線程 「主要」 java.lang.NoClassDefFoundError:組織/ dom4j中/ IO/SAXReader依靠

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中。任何想法如何解決這個問題?

+0

嘗試在命令 – MozenRath

回答

1

您需要使用相同的類路徑,同時運行以及

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

添加一個分號類路徑decalration後,當我使用的語法完全像你的建議,我得到: 的java -cp /usr/share/java/dom4j.jar ;.主要 用法:java [-options] class [args ...] (執行類) 或java [-options] -jar jarfile [args ...] (執行jar文件) .. ........ -bash:Main:沒有這樣的文件或目錄 – kamal

相關問題