怎么处理java.lang.NoClassDefFoundError

rgao906 2003-01-22 11:05:12
我刚从JAVA转到JSP,在做一个读写XML的程序.由一个SERVELET,一个BEAN和一个HTML文件组成,文件编译成功,但运行时递交表单后报错

错误内容为
java.lang.NoClassDefFoundError: org/apache/xml/serialize/BaseMarkupSerializer

请问是什么原因.
...全文
954 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
rgao906 2003-01-22
  • 打赏
  • 举报
回复
谢!但我已经import了,而且也可以成功编译.这是我的servlet程序,请帮忙看一看.

import java.beans.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

import javax.xml.parsers.*;
import org.w3c.dom.*;

import org.apache.xml.serialize.OutputFormat; //formating and serializing xml output
import org.apache.xml.serialize.Serializer;
import org.apache.xml.serialize.SerializerFactory;
import org.apache.xml.serialize.XMLSerializer;

public class BookServlet extends HttpServlet {

//add new book into session object
private boolean insertBookToSession(HttpServletRequest request) throws ServletException, IOException {
String title = request.getParameter("title");
String author = request.getParameter("author");
String isbn = request.getParameter("isbn");
String publisher = request.getParameter("publisher");
String publisherCountry = request.getParameter("publisherCountry");

BookBean newBook = new BookBean(title, author, isbn, publisher, publisherCountry);

HttpSession session= request.getSession(true); //retrieve session object

Object books = session.getAttribute("BOOKLIST"); //retrieve "BOOKLIST" from session object
//"BOOKLIST" is an array of BookBean
if (books == null) { //if no booklist stored in seession object
BookBean [] newbooklist = new BookBean [10]; //create a new booklist with 10 elements

newbooklist[0] = newBook; //add first book to the lis
session.setAttribute("BOOKLIST", newbooklist); //insert the booklist into session object

return true;
}
else {
BookBean [] booklist = (BookBean []) books;
int newbookindex = 10; //if newbookindex = 10 : represents book list is full
for (int i=0; i<4; i++) {
if (booklist[i] == null) {
newbookindex = i;
break;
}
}

if (newbookindex == 10) { //booklist is full
return false;
}
else { //booklist is NOT full
booklist[newbookindex] = newBook;
session.setAttribute("BOOKLIST", booklist);

return true;
}
}
}

private Document generateDOM(HttpServletRequest request) throws ServletException, IOException {
DocumentBuilderFactory factory;
DocumentBuilder builder;

try {
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException pe) {
throw new IOException("ParserConfiguration problem");
}

Document document = builder.newDocument();
Element root = document.createElement("booklist");
document.appendChild(root);


HttpSession session= request.getSession(false); //assume there is always a session exist
Object books = session.getAttribute("BOOKLIST"); //assume there is always an attribute contain a list of book
//you should do proper checking here in your project.
BookBean [] booklist = (BookBean []) books; //assume the list contains at most 10 books

int bookcount = 0;
for (int i=0; i<booklist.length; i++) {
if (booklist[i] != null)
bookcount++;
else
continue;
}

//creating the domcument tree that store the records in "BOOKLIST"
for (int j=0; j<bookcount; j++) {
Element bookElement = document.createElement("book");

Element bookTitleElement = document.createElement("title");
Text textnode1 = document.createTextNode(booklist[j].getTitle());
bookTitleElement.appendChild(textnode1);
bookElement.appendChild(bookTitleElement);

Element bookAuthorElement = document.createElement("author");
Text textnode2 = document.createTextNode(booklist[j].getAuthor());
bookAuthorElement.appendChild(textnode2);
bookElement.appendChild(bookAuthorElement);

Element bookISBNElement = document.createElement("isbn");
Text textnode3 = document.createTextNode(booklist[j].getIsbn());
bookISBNElement.appendChild(textnode3);
bookElement.appendChild(bookISBNElement);

Element bookPublisherElement = document.createElement("publisher");
Element publisherNameElement = document.createElement("name");
Text textnode4 = document.createTextNode(booklist[j].getPublisher());
publisherNameElement.appendChild(textnode4);
bookPublisherElement.appendChild(publisherNameElement);

Element publisherCountryElement = document.createElement("country");
Text textnode5 = document.createTextNode(booklist[j].getPublisherCountry());
publisherCountryElement.appendChild(textnode5);
bookPublisherElement.appendChild(publisherCountryElement);
bookElement.appendChild(bookPublisherElement);

root.appendChild(bookElement);
}
return document;
}

public void generateXMLDocument (Document document) throws IOException {
File newXMLFile = new File("..//webapps//book//xml//booklist.xml");
FileWriter xmlwriter = new FileWriter(newXMLFile);
StringWriter stringOut = new StringWriter(); //Writer will be a String

OutputFormat xmlformat = new OutputFormat(document);
xmlformat.setIndenting(true);

XMLSerializer serializer1 = new XMLSerializer(xmlwriter, xmlformat);
XMLSerializer serializer2 = new XMLSerializer(stringOut, xmlformat);

serializer1.asDOMSerializer();
serializer2.asDOMSerializer();

serializer1.serialize(document.getDocumentElement());
serializer2.serialize(document.getDocumentElement());

System.out.println( "" + stringOut.toString() );
System.out.println("Output completed");
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");

if (action != null && action.equals("SAVE")) {
if (insertBookToSession(request)) {
response.sendRedirect("../jsp/Book.html");
}
else {
response.sendRedirect("../jsp/ListFullError.html");
}
}
else if (action != null && action.equals("CREATE XML")) {
Document newDOMTree = generateDOM(request);

generateXMLDocument(newDOMTree);

HttpSession session= request.getSession(true); //retrieve session object and remove "BOOKLIST"
session.removeAttribute("BOOKLIST");
response.sendRedirect("../jsp/Book.html");
}
}
}
xchen1 2003-01-22
  • 打赏
  • 举报
回复
类没有找到
1、确认是否引入org.apache.xml.serialzie.BaseMarkupSerializer
2、确认这个类是否存在相应的路径中
zxhong 2003-01-22
  • 打赏
  • 举报
回复
bean和servlet要放在jsp文件所在目录下\web-inf\classes\package(if you have package in your java class)\*.class
zhu_liping 2003-01-22
  • 打赏
  • 举报
回复
java.lang.NoClassDefFoundError
找不到类呀
看看你的CLASSPATH
acefr 2003-01-22
  • 打赏
  • 举报
回复
你要import包,并且确定该包中包含你要用到的类,不然就会出现这样的错误.
NetFan 2003-01-22
  • 打赏
  • 举报
回复
java.lang.NoClassDefFoundError--- Class not Found

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧