title: Different / better approaches for calling python function from Java
body: <p>I am quite new to python and am trying to call python's function from java.</p>
  
  <p>My primary requirements are these:</p>
  
  <ul>
  <li>call should be transparent, in the sense that it should not require modifying <code>.py</code> file simply to enable it to be called from java. I might be given any python file with some functions inside it. I should be able to call any of these functions without requiring to modify <code>.py</code> file.</li>
  <li>I want to be able to send arguments of both primitive types (<code>int</code>, <code>String</code>, <code>floats</code> etc.) or non primitive types (<code>HashMap</code>,<code>ArrayList</code>) from java to python function and receive back the returned object (which may of primitive types or non-primitive types) from python to java. I am also using pandas DataFrame and numpy ndarray and hence also want to be able to send and receive corresponding objects to and from java.</li>
  <li>I <em>preferably</em> want to stick to CPython instead of Jython because I might need to use newer libraries that might not be available in Jython.</li>
  </ul>
  
  <p>There are several options that I found online. Few are:</p>
  
  <ul>
  <li><p><strong>Use Jython's <a href="http://www.jython.org/javadoc/org/python/util/PythonInterpreter.html" rel="nofollow noreferrer">PythonInterpreter</a></strong> with which I am able to invoke python function without requiring any changes to <code>.py</code> script file:</p>
  
  <p><strong>py1.py</strong></p>
  
  <pre><code> def square2(list):
       squares = []
       for i in list:
           squares.append(i*i)
       return squares
  </code></pre>
  
  <p><strong>JythonTest.groovy</strong>    </p>
  
  <pre><code> import org.python.util.PythonInterpreter
   import org.python.core.*;
  
   class JythonTest
   {
        static main(def args)
        {
            PythonInterpreter pi = new PythonInterpreter()
            pi.exec("from py1 import square2")
            PyFunction pf = (PyFunction)pi.get("square2")
            println pf.__call__(new PyList([1,2,3,4]))[2]   //9
        }
   }
  </code></pre>
  
  <p>I am very well able to satisfy my needs. But its not CPython.</p></li>
  <li><p><strong>Use <a href="http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html#jsr-223" rel="nofollow noreferrer">ScriptEngine</a></strong>: This is very similar to PythonInterpreter. But agains its Jython. Also, unlike PythonInterpreter, we cannot work with Jython 2.5+ and cannot directly access PyObjects. So this option can be very well closed.</p></li>
  <li><strong>Use <a href="https://www.py4j.org/index.html" rel="nofollow noreferrer">py4j</a></strong>: Cant find example which is as minimal as in case of Jython PythonInterpreter</li>
  <li><strong>Use <a href="http://pythonhosted.org/javabridge/" rel="nofollow noreferrer">java2python</a></strong>. But <a href="http://pythonhosted.org/javabridge/java2python.html" rel="nofollow noreferrer">not much information is given</a> about calling python from java so that I can conclude whether my above requirements can be satisfied. Can anyone shed more light on this? More specifically if we can write the code as minimal as the one in Jython PythonInterpreter.</li>
  <li><strong>Use <a href="http://jpype.sourceforge.net/" rel="nofollow noreferrer">JPype</a></strong>: However after quick go through I feel I will not be able to write as minimal code as in case of Jython PythonInterpreter. Also I felt the project somewhat not under developement. Is it?</li>
  </ul>
  
  <p>It seems that Jython PythonInterpreter is the best choice if I have understood all above approaches correctly. Have I made mistakes while grasping them? Also is there any other better option?</p>