0

尝试从 servlet 写入项目中的本地 xml 文件。当从另一个 java 类运行相同的代码时,不会发生错误,但是当代码从 servlet 运行时,我收到以下错误:

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;

谢谢您的帮助

4

1 回答 1

1

检查jdom-2.0.2.jar(或您使用的任何版本)是否在C:\eclipse\workspace1\BeamII\WebContent\WEB-INF\lib.

于 2012-08-15T18:44:53.140 回答