The problem is you don't have MyClass or MyObject in the Python namespace.
import org.python.util.PythonInterpreter;
import org.python.core.*;
public class Te {
public static void main(String []args) throws PyException
{
PythonInterpreter interp = new PythonInterpreter();
MyClass MyObject = new MyClass(); // A class created by me
// how do I do this?
interp.set( "MyObject", MyObject );
interp.exec("MyObject.doSomething()");
// or
interp.set( "MyClass", MyClass.class );
interp.exec( "MyClass().doSomething()" );
}
}
class MyClass
{
public void doSomething() {}
}
kb
> John Young wrote:
>
> quick question with embedding jython.... in the following example (from the embedding 'tutorial' at jython.org)
> how do I access MyClass from within the python code?
>
>
> import org.python.util.PythonInterpreter;
> import org.python.core.*;
>
> public class SimpleEmbedded {
> public static void main(String []args) throws PyException
> {
> PythonInterpreter interp = new PythonInterpreter();
> MyClass MyObject = new MyClass(); // A class created by me
>
> System.out.println("Hello, brave new world");
> interp.exec("import sys");
> interp.exec("print sys");
>
> // how do I do this?
> // interp.exec("MyObject.doSomething()");
>
> interp.set("a", new PyInteger(42));
> interp.exec("print a");
> interp.exec("x = 2+2");
> PyObject x = interp.get("x");
>
> System.out.println("x: "+x);
> System.out.println("Goodbye, cruel world");
> }
> }
>
> class MyClass
> {
> public void doSomething(){}
> }
>
>
>
>
|