views:

355

answers:

1

Hi guys.

I have a problem when using Jython, but I can't seem to find a solution in the documentation.

Basically what I have is an object that has been instantiated in Java, and I want to instantiate another Java object (in the python script) and have the pre-instatiated java object added to the object I have instantiated in the jython interpreter.

For example:

public class A {
    private B bInstance;

    public void setB(B bval) {
        bInstance = b;
    }
}

public class B {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String n) {
        this.name = n;
    }
}

python script (there is an instance of B bound as "b_inst"):

import com.package.A
a_inst = com.package.A()
a_inst.setB(b_inst)

When I try to run the above code I get the following exception: TypeError: setB(): expected 2 args; got 1

I am pretty sure this is because the setB() method is attempting to call a method on a Python object, not on the actual java object. Basically I expect the call to setB() on the instance of A I have just created in the jython script to be a java object, not a python object.

Sorry if this is obvious, I've read the tutorials as well as the Jython sections of "Java in a Nutshell" and "Core Python Programming", but the examples are really really simple, they have no examples of how to do this two-way binding.

+1  A: 

Which version of Jython are you using? I'm trying this out with 2.5.0 and the following works:

from com.package import A, B
b_inst = B()
a_inst = A()
a_inst.setB(b_inst)

It didn't like the com.package.A() syntax so I changed it to what you see here.

laz
Thanks for your answer Laz. There's a subtle difference between what you've tried and what I am doing:The instance of B is created in java and passed into the script through the PythonInterpreter instance, it isn't created in python like your script does.
Aidos
Aha, I missed that aspect of it! Now I'm even more intrigued :)
laz
Updating to the latest version of Jython appears to have fixed this.
Aidos