Ilija
Ilija

Reputation: 679

Java class from jython

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

Upvotes: 1

Views: 2000

Answers (2)

lothar
lothar

Reputation: 20209

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)");

Upvotes: 5

James McMahon
James McMahon

Reputation: 49629

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.

Upvotes: 0

Related Questions