【发布时间】:2012-03-10 06:49:59
【问题描述】:
可能重复:
Calling Java app with “subprocess” from Python and reading the Java app output
基本上我正在寻找的是,我想在使用 python 运行时与 java 程序进行交互,这样我就可以访问它的输出并将输入传递给它。 我已经设法使用 python 运行 Java 程序。我想知道我可以在我的 python 程序中访问 java 程序的输出。 例如。 在java程序中: System.out.println("输入编号"); 在python中,我应该能够将“Enter no”作为字符串获取,并从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。
标签: java python django linux subprocess