• How to integrate Python in Java ?

    GloriaFine Member

    I’m currently broadening my programming knowledge to integrate different scripting languages into a more robust language like Java. So far I did it with JS and XML, now I’m trying Python.

    I can’t seem to find a clear snippet/tutorial to guide me through it. I’m looking for something that doesn’t just execute a line of code, or a whole file, but a specific method/function declared into a python file (So I can place multiple methods into 1 file, without having to make 20 separate ones).

  • Ganesh Member

    I’m not sure if it’s correct or not (or if there is a better way)

    package test;
    import org.python.util.PythonInterpreter; 
     
    public class ScriptManagerPython { 
     
       PythonInterpreter interpreter = null; 
     
     
       public ScriptManagerPython() { 
          PythonInterpreter.initialize(System.getProperties(), 
                                       System.getProperties(), new String[0]);   
          this.interpreter = new PythonInterpreter(); 
       } 
     
       public void execfile( final String fileName ) { 
          this.interpreter.execfile(fileName); 
       } 
     
       public void executeMethod(final String methodName) {
          this.interpreter.eval(methodName);
       }
       public static void main( String gargs[] ) { 
          ScriptManagerPython pythonManager = new ScriptManagerPython(); 
          pythonManager.execfile("c:/python.py"); 
          pythonManager.executeMethod("Hello().abc()");
       } 
    }
    
Viewing 1 reply thread
  • You must be logged in to reply to this topic.
en_USEnglish