我只是尝试从 servlet 调用基本 Lucene 演示类,但收到此异常:
SEVERE: Servlet.service() for servlet SearcServlet threw exception java.lang.ClassNotFoundException: org.apache.lucene.store.Directory at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491) at servlet.SearcServlet.doPost(SearcServlet.java:50)
我的servlet方法:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
try {
String[] arguments = new String[] {"123"};
SearchFiles.main(arguments);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
当我从另一个 Java 类调用它时,搜索正在执行,但是当我从 servlet 调用它时,我收到此异常。我正在使用 lucene 演示搜索类,不包含其他功能。
最佳答案
java.lang.ClassNotFoundException
与 Java Classpath 相关。当 JVM 尝试在运行时动态加载类时,会出现 ClassNotFoundException
,如果在类路径中找不到该类,则会抛出 java.lang.ClassNotFoundException
。
由于您使用的是名为 Apache Lucene 的第三方 API,因此需要将所有必需的 jar 文件放置到应用程序类路径中。
对于 Web 应用程序,默认类路径是 webapp 或 WebContent 目录下的 /WEB-INF/lib
目录。
将所有必需的 jar 文件复制到那里,Eclipse 会处理剩下的一切。
关于java - Lucene 演示类中的 Servlet 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14638696/