Can we call a python method from java? -


this question has answer here:

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

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -