0

I am using py4j to call Java methods from my python code. When I start the java gateway on eclipse and use python code to call the java methods I get proper response. But when I deploy the same java program as war file on tomcat and then use python code to call the java methods deployed on tomcat I get the following errors.

Java code:

package testjav;

import py4j.GatewayServer;

public class Helloworld {

    public String sayhello() {
        System.out.println("Hi there");
        return "hi";
    }

    public static void main(String[] args) {
        GatewayServer gatewayServer = new GatewayServer(new Helloworld());
        gatewayServer.start();
        System.out.println("Gateway Server Started");
    }
}

Python Code:

    from py4j.java_gateway import JavaGateway, GatewayParameters

    gateway = JavaGateway(gateway_parameters=GatewayParameters(port=8080))

    out = gateway.entry_point.sayhello()
    print(out)

Error on Tomcat:

enter image description here

Error on Python:

Traceback (most recent call last):
  File "C:\Users\******\Documents\*****\helloworld\tasksHello\tasks.py", line 24, in <module>
     out = gateway.entry_point.sayhello()
  File "C:\Users\******\AppData\Local\Continuum\anaconda3\lib\site-packages\py4j\java_gateway.py", line 1160, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "C:\Users\*******\AppData\Local\Continuum\anaconda3\lib\site-packages\py4j\protocol.py", line 318, in get_return_value
    value = OUTPUT_CONVERTER[type](answer[2:], gateway_client)
KeyError: 'T'

I checked this answer variable by looking into py4j module code. In case of jvm run it returns : yshi which is expected as hi is output and ys are used by py4j. But in case of tomcat answer = 'HTTP/1.1'. I don't understand why this is happening. Please guide me.

DSG
  • 183
  • 1
  • 2
  • 8
  • Right now there's very little we can do to help you. Please edit your question to include your Python and Java source code. If the code is too large or cannot be included for other reasons, please spend some time putting together a [Minimal, Complete and Verifiable example](https://stackoverflow.com/help/mcve) that demonstrates the problem. – Luke Woodward Dec 27 '17 at 14:02
  • You will need to provide the code as it looks in the war file: are you really calling the main method in your war file? How is the Py4J code called in Tomcat (I would imagine a servlet is invoking the code whe receiving a GET request)? – Barthelemy Dec 30 '17 at 10:43

0 Answers0