Jython 2.5.1:从 Java 调用 __main__ - 如何传入命令行参数?

发布于 2024-11-17 06:41:38 字数 531 浏览 3 评论 0原文

我在 Java 中使用 Jython;所以我有一个类似于下面的Java设置:

String scriptname="com/blah/myscript.py"
PythonInterpreter interpreter = new PythonInterpreter(null, new PySystemState());
InputStream is = this.getClass().getClassLoader().getResourceAsStream(scriptname);
interpreter.execfile(is);

这将(例如)运行下面的脚本:

# myscript.py:
import sys

if __name__=="__main__":
    print "hello"
    print sys.argv

如何使用此方法传递“命令行”参数? (我希望能够编写 Jython 脚本,以便我也可以使用“python script arg1 arg2”在命令行上运行它们)。

I'm using Jython from within Java; so I have a Java setup similar to below:

String scriptname="com/blah/myscript.py"
PythonInterpreter interpreter = new PythonInterpreter(null, new PySystemState());
InputStream is = this.getClass().getClassLoader().getResourceAsStream(scriptname);
interpreter.execfile(is);

And this will (for instance) run the script below:

# myscript.py:
import sys

if __name__=="__main__":
    print "hello"
    print sys.argv

How I pass in 'commandline' arguments using this method ?
(I want to be able to write my Jython scripts so that I can also run them on the commandline with 'python script arg1 arg2').

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘人皮目录ツ 2024-11-24 06:41:39

我使用的是 Jython 2.5.2,但 runScript 不存在,因此我必须将其替换为 execfile。除了这个区别之外,我还需要在创建 PythonInterpreter 对象之前在状态对象中设置 argv :

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);

最初状态对象中的 argv 列表长度为 1,其中有一个空字符串,因此前面的代码会产生以下输出:

hello
['', 'arg1', 'arg2']

如果您需要 argv[0] 作为实际的脚本名称,则需要创建像这样的状态:

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"));

那么输出是:

hello
['myscript.py', 'arg1', 'arg2']

I'm using Jython 2.5.2 and runScript didn't exist, so I had to replace it with execfile. Aside from that 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 preceding code results in the output:

hello
['', 'arg1', 'arg2']

If you need argv[0] to be the actual script name, you'd need to create the state like this:

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 the output is:

hello
['myscript.py', 'arg1', 'arg2']
傲鸠 2024-11-24 06:41:39

对于上述解决方案不起作用的人,请尝试以下解决方案。这适用于 jython 版本 2.7.0

String[] params = {"get_AD_accounts.py","-server", "http://xxxxx:8080","-verbose", "-logLevel", "CRITICAL"};

上面复制了下面的命令。即每个参数及其值是 params 数组中的单独元素。

jython get_AD_accounts.py -logLevel CRITICAL -server http://xxxxxx:8080 -verbose

PythonInterpreter.initialize(System.getProperties(), System.getProperties(), params);

PySystemState state = new PySystemState() ;

InputStream is = new FileInputStream("C:\\projectfolder\\get_AD_accounts.py");
            PythonInterpreter interp = new PythonInterpreter(null, state);

PythonInterpreter interp = new PythonInterpreter(null, state);
interp.execfile(is);

For those people whom the above solution does not work, try the below. This works for me on jython version 2.7.0

String[] params = {"get_AD_accounts.py","-server", "http://xxxxx:8080","-verbose", "-logLevel", "CRITICAL"};

The above replicates the command below. i.e. each argument and its value is separate element in params array.

jython get_AD_accounts.py -logLevel CRITICAL -server http://xxxxxx:8080 -verbose

PythonInterpreter.initialize(System.getProperties(), System.getProperties(), params);

PySystemState state = new PySystemState() ;

InputStream is = new FileInputStream("C:\\projectfolder\\get_AD_accounts.py");
            PythonInterpreter interp = new PythonInterpreter(null, state);

PythonInterpreter interp = new PythonInterpreter(null, state);
interp.execfile(is);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文