通过 Python 执行 Java 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/702861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:31:56  来源:igfitidea点击:

Executing Java programs through Python

javapython

提问by

How do I do this?

我该怎么做呢?

采纳答案by Andrew Hare

You can execute anything you want from Python with the os.system()function.

您可以使用该os.system()函数从 Python 中执行任何您想要的操作。

os.system(command)
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system, and has the same limitations. Changes to os.environ, sys.stdin, etc. are not reflected in the environment of the executed command.

os.system(command)
在子 shell 中执行命令(字符串)。这是通过调用Standard C函数系统来实现的,也有同样的局限性。os.environ、sys.stdin 等的变化不会反映在执行命令的环境中。

For more power and flexibility you will want to look at the subprocessmodule:

为了获得更多功能和灵活性,您需要查看subprocess模块:

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

subprocess 模块允许您生成新进程,连接到它们的输入/输出/错误管道,并获取它们的返回代码。

回答by Fortyrunner

Of course, Jython allows you to use Java classes from within Python. It's an alternate way of looking at it that would allow much tighter integration of the Java code.

当然,Jython 允许您在 Python 中使用 Java 类。这是另一种看待它的方式,它允许更紧密地集成 Java 代码。