1

I want to make a jar file from a python package. I am using the jython-compile-maven-plugin with maven. The tricky part seems to be the handling of arguments. The receiving python package uses optparse which works fine on the python side but I have difficulties to provide the parameters via java / jython.

I got an error about missing arguments. Now I tried to provide the arguments to main() but it doesn't expect any.

This is how I call into the jar:

java -jar target/metrics-0.2.0-jar-with-dependencies.jar -f sample.txt --format csv -q

Java started
5 Arguments: -f, sample.txt, --format, csv, -q, 
Exception in thread "main" javax.script.ScriptException: TypeError: main() takes no arguments (1 given) in <script> at line number 1

Any ideas on how to provide the args properly?

here is my InitJython.java:

package org.testingsoftware.metrics;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.apache.commons.lang.StringUtils;

public class InitJython {

    public static void main(String[] args) throws ScriptException {
        System.out.println("Java started");
        System.out.print(args.length + " Arguments: ");
        for (String s : args) {
            System.out.print(s);
            System.out.print(", ");
        }
        System.out.println();
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");

        // c.exec("try:\n  from metrics.metrics import main\n  main()\nexcept SystemExit:\n  pass");
        engine.eval("from metrics.metrics import main");
        engine.eval("main('" + StringUtils.join(args, " ") + "')");
        System.out.println("Java exiting");
    }

    public void run() throws ScriptException {
    }
}
moin moin
  • 2,263
  • 5
  • 32
  • 51
  • This answer might help: http://stackoverflow.com/a/6510157/407651. It shows how to transfer command line arguments using [PythonInterpreter](http://www.jython.org/javadoc/org/python/util/PythonInterpreter.html) and [PySystemState](http://www.jython.org/javadoc/org/python/core/PySystemState.html). – mzjn Sep 25 '13 at 17:47

1 Answers1

0

This line,

engine.eval("main('" + StringUtils.join(args, " ") + "')");

evaluates to

engine.eval("main('-f sample.txt --format csv -q')");

This means that the main() Python function will receive one argument ("1 given", as it says in the error message).

In order to make it work, you could have a main() that looks something like this:

def main(arg):
    # Split 'arg' to get the arguments that were given to InitJython 
    # on the command line as a list of strings
    args = arg.split()

    parser = OptionParser()
    parser.add_option("-f", dest="filename",
                      help="read from FILENAME")
    ...
    ...

    (opts, args) = parser.parse_args(args)
mzjn
  • 48,958
  • 13
  • 128
  • 248
  • the dilemma is that if I change this the package would not work any more for python users. On the python side parser.parse_args() is used. I had hoped that the package could work for both versions. – moin moin Aug 14 '13 at 20:29
  • Judging by the Java code, the `main()` Python function is supposed to take arguments. But apparently it doesn't since the error message says "main() takes **no** arguments (1 given)". How should it work? You haven't shown us any Python code so I am a little confused. – mzjn Aug 15 '13 at 06:58
  • I think in python the handling of command line parameters is special (sys.argv). What I am trying to fix is the Jython wrapper without breaking the python package. Probably this is not really a Java type of question but more on the Jython side. The Python code is contained in a Python package: https://pypi.python.org/pypi/metrics/. – moin moin Aug 15 '13 at 15:02
  • OK, the `metrics.metrics` module is supposed to be run as a script from the command line. The `main()` function in that module does not take any arguments so there is no point in trying to call it with arguments from Java. It seems to be rather difficult to execute a Python script in the [`__main__`](http://docs.python.org/2/library/__main__.html) environment with `ScriptEngine`. I don't know how to solve the problem at this point. – mzjn Aug 15 '13 at 19:50