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:
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.