1

I am trying to execute my python script in java using jython. The important thing is that I need to pass command line arguments to my script using jython, e.g. myscript.py arg1 arg2 arg3. There is a similar question here: Passing arguments to Python script in Java

Which was not answered completely (none of the solutions work).

My code now looks like this:

String[] arguments = {"arg1", "arg2", "arg3"}; 
PythonInterpreter.initialize(props, System.getProperties(), arguments);
org.python.util.PythonInterpreter python = new org.python.util.PythonInterpreter();
StringWriter out = new StringWriter();
python.setOut(out);
python.execfile("myscript.py");
String outputStr = out.toString();
System.out.println(outputStr);

However, this does not seem to pass any arguments to the python script. Any suggestions how to do it properly? It must be simple, but I can't find any documentation on the web.

I am using python 2.7 and jython 2.7.0.

Community
  • 1
  • 1

2 Answers2

4

In the meantime, I have found a solution. Here it is:

String[] arguments = {"myscript.py", "arg1", "arg2", "arg3"};
PythonInterpreter.initialize(System.getProperties(), System.getProperties(), arguments);
org.python.util.PythonInterpreter python = new org.python.util.PythonInterpreter();
StringWriter out = new StringWriter();
python.setOut(out);
python.execfile("myscript.py");
String outputStr = out.toString();
System.out.println(outputStr);

What I needed to do was simply to add my script to the passed parameters as arg[0] (see the first line of the code)!

2

Here a small code to do so:

import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.__builtin__;
import org.python.util.PythonInterpreter;

public class JythonTest {

    public static void main(String[] args) {
        PythonInterpreter interpreter = new PythonInterpreter();
        String fileUrlPath = "/path/to/script";
        String scriptName = "myscript";
        interpreter.exec("import sys\n" + "import os \n" + "sys.path.append('" + fileUrlPath + "')\n"+ "from "+scriptName+" import * \n");
        String funcName = "myFunction";
        PyObject someFunc = interpreter.get(funcName);
        if (someFunc == null) {
            throw new Exception("Could not find Python function: " + funcName);
        }
        try {
            someFunc.__call__(new PyString(arg1), new PyString(arg2), new PyString(arg3));
        } catch (PyException e) {
            e.printStackTrace();
        }
    }
}

this call a python script in directory /path/to/script which is called myscript.py.

an example of myscript.py:

def myscript(arg1,arg2,arg3):
    print "calling python function with paramters:"
    print arg1
    print arg2
    print arg3
Stefano
  • 3,981
  • 8
  • 36
  • 66
  • it says "void is an invalid type" for this variable. I am new to java, should it be some other type? – yulia grishina Mar 29 '16 at 14:36
  • and wait... what is the name of the function you want to call? i do not thin that you want directly to call the script... otherwise ther eis no need to use Jython... simply use a system call – Stefano Mar 29 '16 at 14:40
  • i want to call the script directly. i have to use jython (and it's not my choice...). the main reason is that apparently if you use jython it does not matter whether python is installed on your local machine or not. – yulia grishina Mar 29 '16 at 14:42
  • still getting this error: Exception in thread "main" java.lang.Error: Unresolved compilation problems: void is an invalid type for the variable myFunctionJavaWrapper Syntax error on token "(", ; expected Syntax error on token ",", ; expected Syntax error on token ",", ; expected Syntax error, insert ";" to complete LocalVariableDeclarationStatement – yulia grishina Mar 29 '16 at 14:44