2016-11-03 146 views
2

我試圖將可變參數從python傳遞給Java代碼。使用Py4j將可變參數從Python傳遞給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

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代碼 test.py:

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

錯誤:

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)*** 

有沒有辦法將可變參數傳遞給Java?我想通過字符串作爲getLogDebugCmd()的第二個參數也出錯。

回答

0

可變參數實際上表現爲一個數組,所以你可以這樣做:

# 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) 

有一個pull request改善可變參數在Py4J支持,但它有一些編譯問題,因此尚未合併。

+0

感謝您的快速回復,@Barthelemy!這有幫助。 – Pooja

+0

在傳遞'None' - >將None轉換爲'Object'類型之前,只需傳遞一個校正值來代替Object ... args。作爲一種使Java清楚我們傳遞Null對象而不是null來替代Object數組的方法。後者將導致NullPointerException。參考:[鏈接](http://stackoverflow.com/questions/4028059/calling-java-varargs-method-with-single-null-argument)。 例如: 'object_array [0] =無 logDebugCmd = gateway.entry_point.getLogDebugCmd( 「步驟01 - 測試啓動。」,object_array)' – Pooja

+0

實際上,這取決於所謂的代碼:如果它檢查是否ARGS是null,你不會得到NullPointerException。現在想起來,你最好創建一個空數組:object_array = gateway.new_array(object_class,0)。 – Barthelemy