4

I have a java facade class I'm trying to access from python so I decided to use JPype. My facade class only has one constructor (no default) with four args

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

I can't seem to get the types correct when initializing a new instance of the class. Everything I try gives the same error:

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

I know JPype is working. I've tried several combinations of wrappers to get the data in the right form with no luck.

Relevant code:

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()
bnjmn
  • 4,508
  • 4
  • 37
  • 52
  • I don't know python or that jpype thing but: are you sure that `JArray(JString)` is the correct thing for `List`? Sounds to me like it would represent a `String[]` – zapl Nov 30 '12 at 01:53
  • Good question. As I said, I tried several things including it without the wrapper (e.g., `Facade(s1,s2,s3,s4)`) as well as implementing something with `JProxy` which JPype's way of handling java interfaces. If anyone has experience with that, please let me know. – bnjmn Nov 30 '12 at 04:13
  • @bnjmn Can you please share a working sample of executing class' function of a jar from Python using Jpype. I was not able to find a working sample, tried a couple of with options but fail with error 'not callable' – Sankalp Jan 22 '17 at 19:32

1 Answers1

6

JArray(JString) won't match List. You have to use jpype.java.util.ArrayList() (or anything that implements List).

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

So your code will look like that:

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