|
From: Subodh Garg-S. <sub...@ss...> - 2015-12-29 22:25:55
|
Hi All,
Here is the example Python code in hello.py and Java code. I am able to invoke run function of Python class Hello as given the code. I need help in calling "run2" function of Python in Java.
Thanks,
Subodh
class Hello:
__gui = None
def __init__(self, gui):
self.__gui = gui
def run(self):
print 'Hello world!'
def run2(myname):
print myname
Here is my Java code:
import org.python.core.PyInstance;
import org.python.core.PyString;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import java.lang.*;
import java.util.*;
public class InterpreterExample
{
PythonInterpreter interpreter = null;
public InterpreterExample()
{
PythonInterpreter.initialize(System.getProperties(),
System.getProperties(), new String[0]);
this.interpreter = new PythonInterpreter();
}
void execfile( final String fileName )
{
this.interpreter.execfile(fileName);
}
PyInstance createClass( final String className, final String opts )
{
return (PyInstance) this.interpreter.eval(className + "(" + opts + ")");
}
public static void main( String gargs[] )
{
InterpreterExample ie = new InterpreterExample();
PyInstance hello = ie.createClass("Hello", "None");
hello.invoke("run");
}
}
|