tags:

views:

335

answers:

2

Hi, I need to access the java class which is running jython script from that script? Any help?

update: Something like this:

//JAVA CLASS
class Test{
     public String text;
     public Test
     {
        PythonInterpreter pi = new PythonInterpreter(null);
        pi.execfile("test.py");

     }

}

So int test.py I need to do something to change the value of text in Test class

#test.py
doSomething()
Text.test = "new value"

Hope it is more clear

A: 

You have to import your test class at the top of your Jython code. I believe this would be something along the lines of

from com.examplepackage import Test

You'll also to set your text value as static, or pass the Java object into the Jython method.

Check out the article here.

James McMahon
+3  A: 

To pass a java class instance to the embeded jython you need to do:

PythonInterpreter interp = new PythonInterpreter();
    interp.set("a", this);
    interp.exec("a.test = 'new value'");

If you want to call a function (that takes the instance as an argument) from a external script:

 PythonInterpreter interp = new PythonInterpreter();
    interp.set("a", this);
    interp.exec("import externalscript");
    interp.exec("externalscript.function(a)");
lothar