Can we call a python method from java? -
this question has answer here:
- calling python in java? 9 answers
i know jython allows call java method java's classfile if written python, reverse possible ???
i have many algorithms written in python, work pretty python , jython lack proper gui. planing bring gui java , keep python library intact. not able write gui jython or python , cannot write algorithm python. solution found merge java's gui , python's library. possible. can call python's library java.
yes, can done . done creating pythoninterpreter
object , calling python class using .
consider following example :
java :
import org.python.core.pyinstance; import org.python.util.pythoninterpreter; 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(); ie.execfile("hello.py"); pyinstance hello = ie.createclass("hello", "none"); hello.invoke("run"); } }
python :
class hello: __gui = none def __init__(self, gui): self.__gui = gui def run(self): print 'hello world!'
Comments
Post a Comment