无法使用eclipse在动态Web项目中编译servlet

时间:2013-10-05 16:03:09

标签: java servlets eclipse-juno

我是JSP和servlets的新手。我确实试图在这个网站上找到关于这个问题的更多信息,但答案没有帮助。所以这是我的问题。

我正在使用eclipse juno

当我创建一个Java项目并尝试运行我的类时,它运行得很好。

但是现在当我创建一个动态Web项目并尝试运行我的servlet时,我得到以下错误:

 java.lang.NoClassDefFoundError: com/pdfGeneration
Caused by: java.lang.ClassNotFoundException: com.pdfGeneration
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
Exception in thread "main" 

我在buldpath中添加了所有必需的jar,也在WebInf-> lib文件夹下添加了。

下面是我正在尝试在动态Web项目下运行的servlet

package com;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.StringReader;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.jsoup.Jsoup;
import com.itextpdf.text.Chunk;
//import org.jsoup.Jsoup;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

public class pdfGeneration{

    public static final float[][] COLUMNS = {
         { 36, 36, 270, 806 } , { 300, 36, 550, 806 }

        };


  public static void main(String[] args) throws IOException, DocumentException, ClassNotFoundException, SQLException {
    new pdfGeneration().createPdf("differentcolumns.pdf");
  }

  public void createPdf(String filename) throws IOException, DocumentException, ClassNotFoundException, SQLException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(filename));
    document.open();
    PdfContentByte canvas = writer.getDirectContent();
    ColumnText ct = new ColumnText(canvas);

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test3", "root", "root");
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery("select * from exam2");
    int size=0;
    while (rs.next()){
    size++;
    };

    ResultSet rs1=st.executeQuery("select * from exam2");
    String[] myStringArray = new String[size];
    int i=0;
    while (rs1.next()){
       myStringArray[i]=rs1.getString("paper");
      // System.out.println(rs1.getString("paper"));
       i++;
 }

    int no=1;
    int x=1;
    int y=800;
    for (String article : myStringArray) {


         //s=Jsoup.parse(article).text();




    //for (int k=0;k<i;k++) {
        String s=Jsoup.parse(article).text();
     // ct.addElement(new Paragraph(String.valueOf(++k) + ": quick brown fox jumps over the lazy dog"));
      ct.addElement(new Paragraph(s));
      ct.addElement(Chunk.NEWLINE);


     /* else{
          absText(article,x,y); 
          no++;
          y=y-50;
      }*/

     //ct.addElement(new Paragraph(XMLWorkerHelper.getInstance().parseXHtml(writer, document, new StringReader(article))));
      //document.close();
    }
    int column = 0;
    int status = ColumnText.START_COLUMN;

    System.out.println(status);
    while (ColumnText.hasMoreText(status)) {
     // addNotes(canvas);
      ct.setSimpleColumn(
              COLUMNS[column][0], COLUMNS[column][1],
              COLUMNS[column][2], COLUMNS[column][3]);
      //ct.setYLine(COLUMNS[column][3]);
      status = ct.go();



      column = Math.abs(column - 1);
      System.out.println("column "+column);
      if (column == 0)
          document.newPage();

    } 
    document.close();
  }

}

0 个答案:

没有答案