ClassNotFoundException - 如何使用没有maven的外部库?

时间:2018-02-19 14:52:48

标签: java eclipse exception import dependencies

由于我之前提出的问题,我按照以下步骤将Apache PDFbox包含在maven:

  1. download site of PDFbox下载.jars(所有子项目的图书馆下)
  2. maven repository(公共日志和字体框,但字体框已包含在步骤1中)从PDFbox的依赖项下载.jars
  3. 将下载的.jars放入文件夹(C:\PDFjars
  4. 在eclispe中配置了构建路径(将所有.jars添加为外部库)现在看起来像这样:enter image description here
  5. 我的servlet:

    package servlets;
    // other imports    
    import org.apache.pdfbox.pdmodel.PDDocument;
    
    @WebServlet("/print")
    public class PrintServlet extends HttpServlet {
        public PrintServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // From a PDFbox Tutorial
    
            //Creating PDF document object 
              PDDocument document = new PDDocument();    
    
              //Saving the document
              document.save("C:/PdfBox_Examples/my_doc.pdf");
    
              System.out.println("PDF created");  
    
              //Closing the document  
              document.close();
    
            doGet(request, response);
        }
    
    }
    

    因此,当我启动我的Web项目并调用该函数时,应生成一个简单的PDF文档,我得到了以下错误:

        Servlet.service() for servlet [servlets.PrintServlet] in context with path [/StaticsCalculator] threw exception [Servlet execution threw an exception] with root cause
    java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocument
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1291)
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
        at servlets.PrintServlet.doPost(PrintServlet.java:44)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Unknown Source)
    

    解决方案:我也必须复制WEB-INF/lib文件夹中的.jars。

2 个答案:

答案 0 :(得分:1)

你可以 a)使用本地maven存储库并将依赖项放在那里。 b)在应用程序的类路径中添加依赖项 - 这基本上就是maven为你所做的。

答案 1 :(得分:1)

单独下载jar文件并将其添加到您的代码依赖项中。您可以在Maven Repository上搜索任何Maven文件。如果您正在使用Eclipse,只需修改构建路径以指向外部jar并为其指定jar的位置。这将处理您的库所需的任何依赖项,这将导致依赖性地狱,您必须手动读取pom并下载每个库。