简体   繁体   English

使用Py4j将varargs从Python传递给Java

[英]Passing varargs to Java from Python using Py4j

I am trying to pass varargs to Java code from python. 我试图从vthon将varargs传递给Java代码。

Java code : LogDebugCmd.java Java代码:LogDebugCmd.java

public class LogDebugCmd implements Command {
  private Class clazz;
  private String format;
  private Object[] args;

  public LogDebugCmd() {}
  public void setLog(String format, Object... args) {
    this.format = format;
    this.args = args.clone();
    clazz = getClass();
  }

  @Override
  public void execute() {
    VLog.getLog(clazz).debug(format, args);
  }

CommandEntryPoint.java CommandEntryPoint.java

public class CommandEntryPoint {
  private LogDebugCmd logDebugCmd;

  public CommandEntryPoint() {
    logDebugCmd = new LogDebugCmd();

  public LogDebugCmd getLogDebugCmd(String str, Object... args) {
    setLog(str, args);
    return logDebugCmd;
  }
  public static void main(String args[]) {
    GatewayServer gatewayServer = new GatewayServer(new CommandEntryPoint());
    gatewayServer.start();
    System.out.println("Gateway Server Started");
  }

Python code test.py: Python代码test.py:

from py4j.java_gateway import JavaGateway
gateway = JavaGateway()
logDebugCmd = gateway.entry_point.getLogDebugCmd("Step 01 - Test initiated.")
logDebugCmd..execute()

Error : 错误:

Traceback (most recent call last):
  File "C:/Python27/Lib/site-packages/py4j-0.10.3-py2.7.egg/py4j/sampleTLStest.py", line 4, in <module>
    logDebugCmd = gateway.entry_point.getLogDebugCmd("Step 01 - Test initiated.")
  File "C:\Python27\lib\site-packages\py4j-0.10.3-py2.7.egg\py4j\java_gateway.py", line 1133, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "C:\Python27\lib\site-packages\py4j-0.10.3-py2.7.egg\py4j\protocol.py", line 323, in get_return_value
    format(target_id, ".", name, value))
***Py4JError: An error occurred while calling t.getLogDebugCmd. 
Trace:
py4j.Py4JException: Method getLogDebugCmd([class java.lang.String]) does not exist at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326)
at py4j.Gateway.invoke(Gateway.java:272)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:214)
at java.lang.Thread.run(Thread.java:745)***

Is there a way to pass varargs to Java ? 有没有办法将varargs传递给Java? I am trying to pass String as the second argument to getLogDebugCmd() which also errors out. 我试图将String作为getLogDebugCmd()的第二个参数传递,这也是错误输出。

Varargs are actually represented as an array so you can do this: Varargs实际上表示为一个数组,所以你可以这样做:

# Pass a None if you have no varargs
logDebugCmd = gateway.entry_point.getLogDebugCmd("Step 01 - Test initiated.", None)

# Create an array of objects
object_class = gateway.jvm.Object
object_array = gateway.new_array(object_class, 2)
object_array[0] = "foo"
object_array[1] = "bar"
logDebugCmd = gateway.entry_point.getLogDebugCmd("Step 01 - Test initiated.", object_array)

There is a pull request to improve varargs support in Py4J, but it has some compilation issues so it has not been merged yet. 有一个拉取请求来改进Py4J中的varargs支持,但是它有一些编译问题所以它尚未合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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