可能重复:
Calling Java app with “subprocess” from Python and reading the Java app output交互与使用python java程序
基本上就是我期待的是,我想同时使用python的运行,所以我可以用java程序进行交互访问其输出并将输入传递给它。 我已经设法使用python运行Java程序。我想知道我可以在我的python程序中访问java程序的输出。例如, 。在java程序中:System.out.println(“Enter no。”); 在Python中我应该能够得到“输入no” string和也python.a
传递价值,java程序我能够做到的,直到没有: Python程序:
import sys
import os.path,subprocess
def compile_java(java_file):
subprocess.check_call(['javac', java_file])
def execute_java(java_file):
java_class,ext = os.path.splitext(java_file)
cmd = ['java', java_class]
subprocess.call(cmd, shell=False)
def run_java(java_file):
compile_java(java_file)
execute_java(java_file)
的Java计划:
import java.io.*;
import java.util.*;
class Hi
{
public static void main(String args[])throws IOException
{
Scanner t=new Scanner(System.in);
System.out.println("Enter any integer");
int str1=t.nextInt();
System.out.println("You entered"+str1);
}
}
感谢名单:)
如果您想将输入传递给程序,您可能需要使用'subprocess.Popen'。 – Amber 2012-02-19 19:38:49
请参阅:http://stackoverflow.com/questions/2388423/calling-java-app-with-subprocess-from-python-and-reading-the-java-app-output – BeRecursive 2012-02-19 19:42:01