簡體   English   中英

為什么我得到NoClassDefFoundError?

[英]Why am I getting a NoClassDefFoundError?

有誰能告訴我為什么我會收到這個錯誤,以及如何解決這個問題?

org.codehaus.staxmate.dom.DOMConverter中org.codehaus.staxmate.dom.DOMConverter._build(DOMConverter.java:188)中的線程“main”java.lang.NoClassDefFoundError:org / codehaus / stax2 / ri / Stax2ReaderAdapter中的異常.buildDocument(DOMConverter.java:171)org.codehaus.staxmate.dom.DOMConverter.buildDocument(DOMConverter.java:152)位於xmlprocessing的org.codehaus.staxmate.dom.DOMConverter.buildDocument(DOMConverter.java:131)。 api.STAXModifyCV.main(STAXModifyCV.java:68)由java.security上的java.net.URLClassLoader $ 1.run(URLClassLoader.java:202)中的java.lang.ClassNotFoundException:org.codehaus.stax2.ri.Stax2ReaderAdapter引起的位於java..L.Liscuncal的java.lang.ClassLoader.loadClass(ClassLoader.java:307)java.net.URLClassLoader.findClass(URLClassLoader.java:190)的.AccessController.doPrivileged(Native Method)$ AppClassLoader.loadClass(Launcher) .java:301)在java.lang.ClassLoader.loadClass(ClassLoader.java:248)... 5更多Java結果:1

我寫了下面的代碼:

    //-*-*-
    FileInputStream input = new FileInputStream("cv.xml");
    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(input);
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    //-*-*- get new entries from input stream
    System.out.println("<< Sahar CV >>\n -> Modify the first reference\n    ** Modify The Name **");
    System.out.print("    Enter degree : ");
    String degree = in.readLine();
    System.out.print("    Enter first name : ");
    String fName = in.readLine();
    System.out.print("    Enter last name : ");
    String lName = in.readLine();
    System.out.println("    ** Modify The Address ** ");
    System.out.print("    Enter new city : ");
    String newCity = in.readLine();
    System.out.print("    Enter new country : ");
    String newCountry = in.readLine();

    //-*-*- let the reader point at the first "reference" element
    int eventType;
    boolean ref = false, fname = false;
    while (!ref && reader.hasNext()) {
        eventType = reader.next();
        switch (eventType) {
            case XMLEvent.START_ELEMENT:
                if (reader.getLocalName().equalsIgnoreCase("references")) {
                    ref = true;
                    break;
                }
        }
    }
    System.out.println("I am here");

    //-*-*- start modification
    Document doc = new DOMConverter().buildDocument(reader);
    Element firstRef = (Element)doc.getElementsByTagName("reference").item(0);
    NodeList name = (NodeList)firstRef.getElementsByTagName("name");
    //-*-*- modify the degree (Dr. , Eng. , Dev. ,etc)
    Attr att = (Attr)name.item(0).getAttributes().item(0);
    ((Node)att).setNodeValue(degree);
    //-*-*- modify first name
    NodeList firstName = (NodeList)firstRef.getElementsByTagName("fname");
    NodeList firstNameChilds = (NodeList)firstName.item(0).getChildNodes();
    ((Node)firstNameChilds.item(0)).setNodeValue(fName);
    //-*-*- modify last name
    NodeList lastName = (NodeList)firstRef.getElementsByTagName("lname");
    NodeList lastNameChilds = (NodeList)lastName.item(0).getChildNodes();
    ((Node)lastNameChilds.item(0)).setNodeValue(lName);
    //-*-*- modify city
    NodeList city = (NodeList)firstRef.getElementsByTagName("city");
    NodeList cityChilds = (NodeList)city.item(0).getChildNodes();
    ((Node)cityChilds.item(0)).setNodeValue(newCity);
    //-*-*- modify country
    NodeList country = (NodeList)firstRef.getElementsByTagName("country");
    NodeList countryChilds = (NodeList)country.item(0).getChildNodes();
    ((Node)countryChilds.item(0)).setNodeValue(newCountry);

    reader.close();
    input.close();
    //-*-*- write DOM document
    FileOutputStream out = new FileOutputStream("cv.xml");
    XMLStreamWriter sw = XMLOutputFactory.newInstance().createXMLStreamWriter(out);

    new DOMConverter().writeDocument(doc, sw);
    sw.close();
    out.close();

你需要確保正確的Woodstox在你的道路上。 基本上,您正在使用在該jar中實現的類,但由於jar不在路徑中,因此Java不知道您引用了哪個類。

這意味着找到了一個不包含預期類的.class文件,因為該包與目錄結構不對應,或者因為文件在編譯后被重命名。 還有其他原因,但這是最常見的。

對不起,我對3個答案投了反對,但突然有了疑問,需要仔細檢查我的想法...而且最終比我想象的更復雜。 但是我在這里找到了一個非常完整的答案: http//mindprod.com/jgloss/runerrormessages.html#NOCLASSDEFFOUNDERROR

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM