I am using Jython 2.5.2 and runScript does not exist, so I had to replace it with execfile . Besides this difference, I also needed to set argv in the state object before creating the PythonInterpreter object:
String scriptname = "myscript.py"; PySystemState state = new PySystemState(); state.argv.append (new PyString ("arg1")); state.argv.append (new PyString ("arg2")); PythonInterpreter interpreter = new PythonInterpreter(null, state); InputStream is = Tester.class.getClassLoader().getResourceAsStream(scriptname); interpreter.execfile (is);
The argv list in the state object initially has a length of 1, with an empty string in it, so the previous code displays the result:
hello ['', 'arg1', 'arg2']
If you need argv[0] be the actual name of the script, you need to create the state as follows:
PySystemState state = new PySystemState(); state.argv.clear (); state.argv.append (new PyString (scriptname)); state.argv.append (new PyString ("arg1")); state.argv.append (new PyString ("arg2"));
Then output:
hello ['myscript.py', 'arg1', 'arg2']
source share