JPype将args传递给Java

我有一个java外观类我试图从python访问,所以我决定使用JPype.我的facade类只有一个带有四个args的构造函数(没有默认值)

public facade(String a, String b, List<String> c, List<String> d){
    ...
}

在初始化类的新实例时,我似乎无法使类型正确.我尝试的一切都给出了同样的错误:

File ".../main.py", line 34, in __init__
    facadeinstance = Facade(jpype.JString(s1), jpype.JString(s2),jpype.JArray(jpype.java.lang.String, 1)(s3), jpype.JArray(jpype.java.lang.String, 1)(s4))
File "/usr/local/lib/python2.7/dist-packages/jpype/_jclass.py", line 79, in _javaInit
    self.__javaobject__ = self.__class__.__javaclass__.newClassInstance(*args)
RuntimeError: No matching overloads found. at src/native/common/jp_method.cpp:121

我知道JPype正在工作.我已经尝试了几种包装器组合,以正确的形式获取数据而没有运气.

相关代码:

import jpype

s1 = "something"
s2 = "something else"
s3 = ["something in a list"]
s4 = ["Something else in a list"]

jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.class.path=" + JavaJarDir)
myLib = jpype.JPackage('myLib')
Facade = myLib.Facade # class loads fine, resources printed to stdout
# The error occurs on the next line
FacadeInstance = Facade(jpype.JString(s1), jpype.JString(s2), jpype.JArray(jpype.java.lang.String, 1)(s3), jpype.JArray(jpype.java.lang.String, 1)(s4))
jpype.shutdownJVM()

解决方法:

JArray(JString)将不匹配List.您必须使用jpype.java.util.ArrayList()(或任何实现List的东西).

myArray = ["A", "B", "C"]
myList = jpype.java.util.ArrayList()
for s in myArray:
    myList.add(s)

所以你的代码看起来像这样:

import jpype

s1 = "something"
s2 = "something else"
s3 = ["something in a list"]
s4 = ["Something else in a list"]

jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.class.path=" + JavaJarDir)

# Import Java library and class
myLib = jpype.JPackage('myLib')
Facade = myLib.Facade

# Prepare List<String> arguments
arg3 = jpype.java.util.ArrayList()
for s in s3:
    list3.add(s)
arg4 = jpype.java.util.ArrayList()
for s in s4:
    list4.add(s)

FacadeInstance = Facade(jpype.JString(s1), jpype.JString(s2), arg3, arg4)

jpype.shutdownJVM()
上一篇:jpype._jexception.RuntimeExceptionPyRaisable: java.lang.RuntimeException: Class Test not found


下一篇:从python访问JVM