我使用 py4j 创建了一个应用程序,它可以使用 java 应用程序将数据从 python 保存到 SQL 数据库中,当我将 JVM 作为应用程序运行时,一切都工作得很好,并且它实际上保存了数据。但是当我在服务器中运行代码时,它给了我一个异常。因此,我想也许我的服务器(Wildfly)和 Py4j 使用相同的端口,所以我按照 turotial 的建议更改了默认的 py4j 端口,这就是 python 双方的方式修改后的样子:
from py4j.java_gateway import JavaGateway, GatewayParameters
gateway = JavaGateway(GatewayParameters(port=25335))
testBD = gateway.entry_point
DBin = gateway.jvm.com.packtpub.wflydevelopment.ch.Application(10,3) #calling constructor
testBD.create(DBin)
但我仍然有一个异常(exception):
Traceback (most recent call last):
File "C:\Users\user\Desktop\test.py", line 4, in
DBin = gateway.jvm.com.packtpub.wflydevelopment.ch.Application(10,3)
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\site-packages\py4j-0.9-py3.5.egg\py4j\java_gateway.py", line 1185, in getattr
answer = self._gateway_client.send_command(
AttributeError: 'GatewayParameters' object has no attribute 'send_command'
如有任何建议,我们将不胜感激。
最佳答案
我从 bartdag 的问题中得到了答案 https://github.com/bartdag/py4j/issues/180 ,他指出将 GatewayParameter 实例指定为参数“gateway_parameters”是可行的。
# This produces the error
gateway = JavaGateway(GatewayParameters(address='192.168.99.100', port=25333))
但是添加参数名称使其可以工作:
# This solves it the error
gateway = JavaGateway(gateway_parameters=GatewayParameters(address='192.168.99.100', port=25333))
关于java - Py4j 在服务器中运行应用程序时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34271776/