简体   繁体   中英

No Class Def Found while using PDFParser in a servlet. but working in Java Application

I am trying to write a servlet that read a uploaded pdf file, and then read it from another servlet. I want to parse that pdf file and search a keyword in the parsed text.

First I did application like a normal java code then It worked fine. But when I did the same code as a servlet Its showing an unexpected error class definition not found error.

Here is the Error:

SEVERE: Servlet.service() for servlet [ex.sat.com.PDFTestServlet] in context with path [/ContentBasedFileRetrival] threw exception [Servlet execution threw an exception] with root cause

java.lang.ClassNotFoundException: org.apache.pdfbox.pdfparser.PDFParser
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at ex.sat.com.PDFTestServlet.pdftoText(PDFTestServlet.java:67)
    at ex.sat.com.PDFTestServlet.doPost(PDFTestServlet.java:48)

I added the pdf box jar file into library of my eclipse project. no compilation error.

But same code is working fine when I do the project as a java application.

here is my servlet:

    package ex.sat.com;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

@WebServlet("/PDFTestServlet")
public class PDFTestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final String SAVE_DIR = "uploadFiles";

/**
 * @see HttpServlet#HttpServlet()
 */
public PDFTestServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html");
    ServletContext context = getServletContext();
    PrintWriter writer = response.getWriter();

    String appPath = request.getServletContext().getRealPath("");
    String savePath = appPath + File.separator+"j2.pdf";
    writer.println(savePath);
    String pdf_text=pdftoText(savePath);

    writer.println("SATYA");
    writer.println(pdf_text);
}

static String pdftoText(String fileName) {
    PDFParser parser;
    String parsedText = null;;
    PDFTextStripper pdfStripper = null;
    PDDocument pdDoc = null;
    COSDocument cosDoc = null;
    File file = new File(fileName);
    if (!file.isFile()) {
        System.err.println("File " + fileName + " does not exist.");
        return null;
    }       
    try {
        parser = new PDFParser(new FileInputStream(file));
    } catch (IOException e) {
        System.err.println("Unable to open PDF Parser. " + e.getMessage());
        return null;
    }
    try {
        parser.parse();
        cosDoc = parser.getDocument();
        pdfStripper = new PDFTextStripper();
        pdDoc = new PDDocument(cosDoc);
        pdfStripper.setStartPage(1);
        pdfStripper.setEndPage(5);
        parsedText = pdfStripper.getText(pdDoc);
    } catch (Exception e) {
        System.err
                .println("An exception occured in parsing the PDF Document."
                        + e.getMessage());
    } finally {
        try {
            if (cosDoc != null)
                cosDoc.close();
            if (pdDoc != null)
                pdDoc.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return parsedText;
}

}

It seems to me your libraries are not being found by the servlet container, you can try putting the jar files on the libraries directory for your container and then restart it. (libraries don't get deployed with applications - normally).

Thank You guys. Just now fond the answer. and Problem Solved. I am doing the build path by adding external jars . Actually I has to add as a library. so in the build path I created a user library and added jar files to the user library, its working fine now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM