【发布时间】:2016-07-17 03:32:18
【问题描述】:
我正在尝试使用 jython 在 java 中执行我的 python 脚本。 重要的是我需要使用 jython 将命令行参数传递给我的脚本,例如myscript.py arg1 arg2 arg3。 这里有一个类似的问题:Passing arguments to Python script in Java
没有完全回答(所有解决方案都不起作用)。
我的代码现在看起来像这样:
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);
但是,这似乎没有将任何参数传递给 python 脚本。 任何建议如何正确地做到这一点? 一定很简单,但我在网上找不到任何文档。
我正在使用 python 2.7 和 jython 2.7.0。
【问题讨论】:
标签: java python command-line-arguments jython