嘗試從servlet寫入位於項目內的本地xml文件。 當同樣的代碼是從另一個Java類運行,不會發生錯誤,但在運行代碼的形式servlet中,我得到以下錯誤:從servlet到達xml
拋出java.lang.ClassNotFoundException:org.jdom2.JDOMException
這是在servletis的doGet方法如下年底粘貼到servlet代碼的一部分:
try {
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("C:\\eclipse\\workspace1\\BeamII\\WebContent\\Data.xml");
Document doc = (Document)builder.build(xmlFile);
Element Users = doc.getRootElement();
List<Element> myUsers = Users.getChildren();
Element beamUser = myUsers.get(0);
Element Data= beamUser.getChildren().get(2);
Element data1= new Element ("data1");
Data.addContent(data1);
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter("C:\\eclipse\\workspace1\\BeamII\\WebContent\\Data.xml"));
// xmlOutput.output(doc, System.out);
System.out.println("done!");
} catch (IOException io) {
io.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
}
我的進口情況如下:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
感謝您的幫助
很明顯,JDom類在運行時不可用。檢查你的WAR文件 - 它是否包含JDom庫? – home 2012-08-15 18:40:51