在python中使用subprocess.Popen运行java主类 [英] running java main class using subprocess.Popen in python
问题描述
我想使用subprocess.Popen()
通过python执行java主类main.java
.main.java
需要 3 个参数.
I want to execute java main class main.java
by python using subprocess.Popen()
. main.java
takes 3 args.
我想知道怎么做?例如我有一个 HelloWorld.java
类:
I wonder how to do it? For example I have a HelloWorld.java
class:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!" + args[0]);
} }
我尝试使用以下命令在 python 中调用它:
I tried to call it in python using the command:
print (subprocess.Popen('java C:/Users/testing/Hello.Main sayHello',shell=True, stdout=subprocess.PIPE).stdout.read())
print (subprocess.Popen('java C:/Users/testing/Hello.Main sayHello', shell=True, stdout=subprocess.PIPE).stdout.read())
其中 'sayHello' 是我想传入的字符串参数.它说
where 'sayHello' is the string args I want to pass in. It said
错误:无法找到或加载主类 C:.Users.testing.Hello.Main
谢谢
推荐答案
您可以通过以下方式运行扩展名为 .class
的 java 文件:
You may run your java file with extension .class
the following way:
java your.path.Main arg1 arg2
哪里,
java
- 命令,运行 Java 解释器your.path.Main
- 类的全名(不含.class
)arg1 arg2
- 参数(由空格或"
之间写入)
java
- command, which runs the Java interpreteryour.path.Main
- full name of your class (without.class
)arg1 arg2
- the arguments (written by spaces or between"
)
此外,当您格式化此行时,它会在 subprocess.Popen()
中作为参数传输.
Further, when you formatted this line, it transmits in subprocess.Popen()
as argument.
subprocess.Popen('java your.path.Main arg1 arg2')
我不是 Python 程序员,因为我建议您阅读有关此方法的文档.
I'm not Python programmer, because I advice you to read documentation about this method.
这篇关于在python中使用subprocess.Popen运行java主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!