简体   繁体   中英

Eclipse: Thrift inside Tomcat Servlet, ClassNotFoundException TException

I'm trying to make a Java servlet that can make Apache Thrift calls, but I'm having trouble starting the servlet.

I have a thrift client , a Java class for making calls to the thrift server

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);
    }

and I have a java servlet which opens a socket through the thrift client

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();

However, when I try to run this servlet (using eclipse and a tomcat server), I get an error like

SEVERE: A child container failed during start

and a ClassNotFoundException for org.apache.thrift.TException

EDIT: All I had to do was include the thrift jars into the Tomcat Server's classpath. See my answer below

I have used the thrift client already without any ClassNotFoundExceptions, and the servlet works on its own as well. However, once I add ThriftClient.openSocket(); into the servlet, it breaks, so I have a feeling Thrift and Tomcat are clashing somehow. Any ideas?

Edit: The weird part is that I never call the method startThrift() but I still get the error.

While you included the thrift jars into your project, you also have to add them to the Tomcat library as well

First make sure your external jars are in your project Java Build Path...

  1. right click your project, click Properties
  2. Under Deployment Assembly , click Add...
  3. Double click Java Build Path Entries... and select the jars/libraries you want to include

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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