在我的下面的 servlet 类中,在 tomcat 服务器中部署时,我收到一些 NoClassDefFoundError:antlr/RecognitionException 和 NoClassDefFoundError:antlr/TokenStreamException 。带有*的星号行是导致它的原因。当我评论这些行时,它很容易部署。我已在 eclipse 的构建路径中包含了必要的 .jar 文件。
基本上在下面的Servlet中我想从网络调用我的java程序。当我从控制台使用 Java 代码时,它不会给出任何错误并且运行得很好。对于我的大多数外部包含的 jar 文件,catalina.out 都在为 NoClassDefFound 哭泣。我是否遗漏了某些内容,或者对于 Servlet,我必须在某个地方指定有关外部 jar 类的信息?
提前致谢..
package i2r.hlt.iris;
import i2r.hlt.iris.utils.JSenti;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.antlr.v4.runtime.RecognitionException;
import antlr.TokenStreamException;
import es.upv.nlel.utils.FileIO;
public class UserInputServlet extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
ServletContext context = config.getServletContext();
homeJsp = context.getRequestDispatcher("/WEB-INF/jsp/iris.jsp");
....
ret.setIndexPath(index_path);
try {
ret.loadUtts();
ret.loadDlgs();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private RequestDispatcher homeJsp, userInput;
private Retrieval ret;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String input = req.getParameter("user");
req.setAttribute("message", input);
homeJsp.forward(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
String input = req.getParameter("user");
List<String> list = new ArrayList<String>();
list.add(input);
Add add = new Add();
String out = null;
/******* try {
***** out = "IRIS: " + FileIO.fileToString(new File(ret.uttMap.get(ret.selectBestUtt(list))));
***** } catch (antlr.RecognitionException e) {
***** throw new ServletException(e);
***** }catch (TokenStreamException e) {
***** throw new ServletException(e);
***** }*/
req.setAttribute("message", out);
homeJsp.forward(req, resp);
}
}
最佳答案
您需要将 ANTLR v2 库放在 Tomcat 的 lib 文件夹中(见注释)。 Java 在特定目录中搜索库。您要使用的库必须位于 Java/Tomcat 可以找到的这些目录之一中。
如果您希望 Eclipse 自动部署依赖项,这可能会有所帮助:
关于java - NoClassDefFoundError:servlet 中的antlr/RecognitionException 但在正常的java 程序中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14768646/