【问题标题】:Eclipse: Thrift inside Tomcat Servlet, ClassNotFoundException TExceptionEclipse:Tomcat Servlet 中的 Thrift,ClassNotFoundException TException
【发布时间】:2013-09-04 03:02:02
【问题描述】:

我正在尝试制作一个可以进行 Apache Thrift 调用的 Java servlet,但我无法启动 servlet。

我有一个 thrift 客户端,一个用于调用 thrift 服务器的 Java 类

public class ThriftClient {

    static TTransport transport;
    static TProtocol protocol;
    static MyService.Client client;

    static long xtk_pointer;

    public static void openSocket() throws TException {
        transport = new TSocket("localhost", 9090);
        transport.open();

        protocol = new TBinaryProtocol(transport);
        client = new MyService.Client(protocol);
    }

我有一个 java servlet,它通过 thrift 客户端打开一个套接字

public class MyServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        out.println("Hello World");
    }

    void startThrift(String [] args) {
        try {
            ThriftClient.openSocket();

但是,当我尝试运行这个 servlet(使用 eclipse 和 tomcat 服务器)时,我得到一个类似

的错误
SEVERE: A child container failed during start

还有一个ClassNotFoundException 代表org.apache.thrift.TException

编辑:我所要做的就是将 thrift jar 包含到 Tomcat 服务器的类路径中。请参阅下面的答案

我已经使用了没有任何 ClassNotFoundExceptions 的 thrift 客户端,并且 servlet 也可以自己工作。但是,一旦我将ThriftClient.openSocket(); 添加到 servlet 中,它就会中断,所以我感觉 Thrift 和 Tomcat 以某种方式发生冲突。有什么想法吗?

编辑:奇怪的是我从不调用方法startThrift(),但我仍然得到错误。

【问题讨论】:

  • 您是否尝试捕获异常?你能发布堆栈跟踪吗?
  • 我正在使用 eclipse 运行 servlet,所以我不完全确定如何捕获异常或打印堆栈跟踪
  • 您可以在 catch 块内调用 exception.printStackTrace(),您应该会在 Eclipse 控制台中看到堆栈跟踪。
  • 似乎主要问题是节俭类TException的ClassNotFoundException,但我之前从未遇到过这个问题......

标签: java eclipse tomcat servlets thrift


【解决方案1】:

当您将 thrift jar 包含到您的项目中时,您还必须将它们添加到 Tomcat 库中

首先确保您的外部 jar 在您的项目 Java 构建路径中...

  1. 右击你的项目,点击Properties
  2. Deployment Assembly 下,单击Add...
  3. 双击 Java Build Path Entries... 并选择您要包含的 jars/库

【讨论】:

    猜你喜欢
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2014-09-09
    • 2014-10-11
    相关资源
    最近更新 更多