简体   繁体   English

在Servlet中使用PDFParser时找不到类定义。 但是在Java应用程序中工作

[英]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. 我正在尝试编写一个servlet,该servlet读取上传的pdf文件,然后从另一个servlet读取它。 I want to parse that pdf file and search a keyword in the parsed text. 我想解析该pdf文件,并在解析的文本中搜索关键字。

First I did application like a normal java code then It worked fine. 首先,我像普通的Java代码一样进行了应用程序,然后运行良好。 But when I did the same code as a servlet Its showing an unexpected error class definition not found error. 但是,当我执行与servlet相同的代码时,它显示了意外的错误类定义,未发现错误。

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. 我将pdf盒jar文件添加到了eclipse项目的库中。 no compilation error. 没有编译错误。

But same code is working fine when I do the project as a java application. 但是当我将项目作为Java应用程序执行时,相同的代码可以正常工作。

here is my servlet: 这是我的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. 在我看来,该servlet容器未找到您的库,您可以尝试将jar文件放在容器的库目录中,然后重新启动它。 (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 . 我通过添加外部jar来构建路径 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. 因此,在构建路径中, 我创建了一个用户库,并将jar文件添加到该用户库,现在可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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