2

I'm trying to use different languages for server and client using Apache Thrift. I wrote the server in Python and it's running without any troubles. I tried to implement the client in Java, as follows:

import org.apache.thrift.TException;
import org.apache.thrift.transport.TSSLTransportFactory;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;

public class Cliente {
    public static void main() {
        try {
            TTransport transport;

            transport = new TSocket("localhost", 9090);
            transport.open();

            TProtocol protocol = new  TBinaryProtocol(transport);
            Calculadora.Client client = new Calculadora.Client(protocol);

            perform(client);

            transport.close();

        } catch (TException x) {
        x.printStackTrace();
        }
    }

    private static void perform(Calculadora.Client client) throws TException {
        System.out.println("Hacemos ping al servidor");
        client.ping();

        // Arrays
        int[] a1 = {2, 5, 9, 6};
        int[] a2 = {1, 3, 4, 10};
        int[] a3 = {2, 1, 3, 4};
        int[] a4 = {5, 7, 5, 4};

        // Array para el producto vectorial
        int[] v1 = {3, 1, 7};
        int[] v2 = {7, 4, 4};

        // Matrices
        int[][] m1 = {{3, 1, 7}, {8, 3, 6}, {8, 5, 2}};
        int[][] m2 = {{2, 5, 7}, {4, 9, 3}, {1, 9, 7}};

        int suma = client.suma(7, 2);
        System.out.println("7+2=" + suma);

        int resta = client.resta(2, 5);
        System.out.println("2-5=" + resta);

        int multp = client.multp(4, 8);
        System.out.println("4*8=" + multp);

        int dividir = client.dividir(9, 2);
        System.out.println("9/2=" + dividir);

        int potencia = client.power(7, 3);
        System.out.println("7^3=" + potencia);

        int modulo = client.modulo(7, 4);
        System.out.println("7%4=" + modulo);

        int[] res1 = client.sumavec(a1, a2);
        System.out.println(res1);

        int[] res2 = client.multpvez(a3, a4);
        System.out.println(res2);

        int[] res3 = client.productovec(v1, v2);
        System.out.println(res3);

        int[][] res4 = client.sumamatrices(m1, m2);
        System.out.println(res4);
     }
    }

I'm trying to compile the Java file using:

javac -cp lib/ Cliente.java

And I'm getting the following errors:

Cliente.java:1: error: package org.apache.thrift does not exist
import org.apache.thrift.TException;
                        ^
Cliente.java:2: error: package org.apache.thrift.transport does not exist
import org.apache.thrift.transport.TSSLTransportFactory;
                                  ^
Cliente.java:3: error: package org.apache.thrift.transport does not exist
import org.apache.thrift.transport.TTransport;
                                  ^
Cliente.java:4: error: package org.apache.thrift.transport does not exist
import org.apache.thrift.transport.TSocket;
                                  ^
Cliente.java:5: error: package org.apache.thrift.transport.TSSLTransportFactory does not exist
import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
                                                       ^
Cliente.java:6: error: package org.apache.thrift.protocol does not exist
import org.apache.thrift.protocol.TBinaryProtocol;
                                 ^
Cliente.java:7: error: package org.apache.thrift.protocol does not exist
import org.apache.thrift.protocol.TProtocol;
                                 ^
Cliente.java:29: error: package Calculadora does not exist
    private static void perform(Calculadora.Client client) throws TException {
                                           ^
Cliente.java:29: error: cannot find symbol
    private static void perform(Calculadora.Client client) throws TException {
                                                                  ^
  symbol:   class TException
  location: class Cliente
Cliente.java:12: error: cannot find symbol
            TTransport transport;
            ^
  symbol:   class TTransport
  location: class Cliente
Cliente.java:14: error: cannot find symbol
            transport = new TSocket("localhost", 9090);
                            ^
  symbol:   class TSocket
  location: class Cliente
Cliente.java:17: error: cannot find symbol
            TProtocol protocol = new  TBinaryProtocol(transport);
            ^
  symbol:   class TProtocol
  location: class Cliente
Cliente.java:17: error: cannot find symbol
            TProtocol protocol = new  TBinaryProtocol(transport);
                                      ^
  symbol:   class TBinaryProtocol
  location: class Cliente
Cliente.java:18: error: package Calculadora does not exist
            Calculadora.Client client = new Calculadora.Client(protocol);
                       ^
Cliente.java:18: error: package Calculadora does not exist
            Calculadora.Client client = new Calculadora.Client(protocol);
                                                       ^
Cliente.java:24: error: cannot find symbol
        } catch (TException x) {
                 ^
  symbol:   class TException
  location: class Cliente
16 errors

I installed thrift compiler using

sudo apt install thrift-compiler

I'm using Ubuntu 18.04. I also installed Thrift following this link: https://thrift.apache.org/download and extracting and using make over the downloaded files.

Further information
I implemented the Client using Python and it's working as normal.

Any help would be appreciated.

1 Answer 1

1

For thrift you need two components:

  1. The thrift compiler
  2. A thrift library

There is only one thrift compiler, which can generate code for all supported target languages. There are however multiple thrift libraries, one for each target language. In your case, you need a Java version of the thrift library. In Ubuntu, this is provided by the libthrift-java package.

I am assuming you have a thrift protocol specification my_protocol.thrift containg:

service Calculadora {
  void ping();
  i32 suma(1: i32 a, 2: i32 b);
  ...
}

You would then use the thrift compiler to generate Java code from this: thrift --gen java my_protocol.thrift. This creates ./gen-java/Calculadora.java.

Now you can compile your Client.java together with the Calculadora.java. Depending on how you installed the thrift library, javac might be able to find it automatically. In that case javac Client.java ./gen-java/Calculadora.java should be sufficient.

If this does not work, you would have to point javac to the jar file containing the thrift library, and also to the jar files containing its dependencies. I do not know where the jar file would be installed on your system, you would have to search for it. In my case, I build the thrift library from source. The resulting jar is located at ./thrift/buildoutput/lib/java/build/libs/libthrift-0.14.0.jar and the dependencies are in ./thrift/buildoutput/lib/java/build/deps/. I then compile with:

javac -cp "./thrift/buildoutput/lib/java/build/deps/*:thrift/buildoutput/lib/java/build/libs/libthrift-0.14.0.jar:." Cliente.java gen-java/Calculadora.java

To run your client, you would then use:

java -cp "./thrift/buildoutput/lib/java/build/deps/*:thrift/buildoutput/lib/java/build/libs/libthrift-0.14.0.jar:." Cliente

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.