2016-11-03 2 views
2

python에서 Java 코드에 varargs를 전달하려고합니다.Py4j를 사용하여 Python에서 Java로 varargs 전달

자바 코드 : 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"); 
    } 

파이썬 코드 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()에 두 번째 인수로 String을 전달하려고하는데 오류도 발생합니다. 이 작업을 수행 할 수 있도록

답변

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) 

가 Py4J에서 변수 인수 지원을 개선하기 위해 pull request있다,하지만 아직 통합되지 않은 있도록 몇 가지 컴파일 문제가 있습니다.

+0

빠른 답장을 보내 주셔서 감사합니다, @ Barthelemy! 이것은 도움이되었습니다. – Pooja

+0

Object ... args 대신에 'None'-> Cast None을 'Object'유형에 전달하여 한 번만 수정합니다. Java에서 Object 배열 대신 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 (- object_array "단계 01 테스트 시작.")' – Pooja

+0

는 사실, 착신 번호에 따라 결정이 인수인지 체크한다 null이면 NullPointerException을 얻을 수 없습니다. 이제 생각해 보면 빈 배열 인 object_array = gateway.new_array (object_class, 0)를 만드는 것이 낫습니다. – Barthelemy

관련 문제