I am posting code for both passing of data from Java -> Python and Python -> Java. Hope this helps someone !!
In this, String array "s" is passed to python script and from python, "city" list is passed from python to Java through function call getData().
JavaProg.java:
import org.python.core.PyInstance;
import org.python.util.PythonInterpreter;
public class JavaProg
{
static PythonInterpreter interpreter;
@SuppressWarnings("resource")
public static void main( String gargs[] )
{
String[] s = {"New York", "Chicago"};
PythonInterpreter.initialize(System.getProperties(),System.getProperties(), s);
interpreter = new PythonInterpreter();
interpreter.execfile("PyScript.py");
PyInstance hello = (PyInstance) interpreter.eval("PyScript" + "(" + "None" + ")");
}
public void getData(Object[] data)
{
for (int i = 0; i < data.length; i++) {
System.out.print(data[i].toString());
}
}
}
PyScript.py:
import JavaProg
class PyScript:
def __init__(self,txt):
city = []
for i in range(0,len(sys.argv)):
city.append(str(sys.argv[i]))
jObj = JavaProg()
jObj.getData(city)
print "Done!"