I am using Jython to run a python script from Java. I am able to send data to the python script as command line arguments when I invoke the script using a PythonInterpreter.
Now I want to call a Java method from within the Jython script, passing a List as a method argument. How do I do that?
-
1How to do execute the command line arg? You can let java listen to the error or output stream of that command line.Reinard– Reinard2016年01月07日 14:26:35 +00:00Commented Jan 7, 2016 at 14:26
-
I want to call java function from python script and send data as arguments. For passing arguments from java to python I used initialized PythonInterpreter objectNikhilS– NikhilS2016年01月07日 14:34:45 +00:00Commented Jan 7, 2016 at 14:34
-
I was supposed to say "How do you execute the command line args" (show code). Sorry didn't get any coffee today.Reinard– Reinard2016年01月07日 14:47:32 +00:00Commented Jan 7, 2016 at 14:47
-
stackoverflow.com/questions/5711084/…Reinard– Reinard2016年01月07日 14:48:04 +00:00Commented Jan 7, 2016 at 14:48
-
Clarity based on OP commentsErick G. Hagstrom– Erick G. Hagstrom2016年01月07日 14:49:17 +00:00Commented Jan 7, 2016 at 14:49
1 Answer 1
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!"