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 {
}
}