简体   繁体   English

使用Python中的“subprocess”调用Java应用程序并阅读Java app输出

[英]Calling Java app with “subprocess” from Python and reading the Java app output

What is the nicest way to read the output (ie via System.out.println) of a Java app which is called from Python with 读取从Python调用的Java应用程序的输出(即通过System.out.println)最好的方法是什么?

subprocess.Popen("java MyClass", shell=True)

without writing and reading a file? 没有写和读文件? (Using Jython etc is not a possible solution) (使用Jython等不是一种可能的解决方案)

p1 = subprocess.Popen(["/usr/bin/java", "MyClass"], stdout=subprocess.PIPE)
print p1.stdout.read() 

I just found the solution: 我刚刚找到了解决方案:

p = subprocess.Popen("java MyClass",
          shell=True,
          stdout=subprocess.PIPE)
output, errors = p.communicate()

S.Mark's is fine too! S.Mark也很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM