0

Trying to write from a servlet to a local xml file located within the project. When the same code is running from another java class, no error occurs but when the code is running form the servlet, I get the following error:

java.lang.ClassNotFoundException: org.jdom2.JDOMException

The part of the code that is pasted into the servlet at the end of the doGet method of the servletis as follow:

            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();
          }

my imports are as follow:

                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;

Thank you for your help

davidfmatheson
  • 3,539
  • 19
  • 27
  • Obviously the JDom classes are not available during runtime. Check your WAR file - does it include the JDom libs? – home Aug 15 '12 at 18:40

1 Answers1

1

Check that jdom-2.0.2.jar (or whatever version you are using) is in C:\eclipse\workspace1\BeamII\WebContent\WEB-INF\lib.

davidfmatheson
  • 3,539
  • 19
  • 27