📜  从 java python 代码示例接收输出

📅  最后修改于: 2022-03-11 14:45:24             🧑  作者: Mango

代码示例1
import os.path,subprocess
from subprocess import STDOUT,PIPE

def compile_java(java_file):
    subprocess.check_call(['javac', java_file])

def execute_java(java_file, stdin):
    java_class,ext = os.path.splitext(java_file)
    cmd = ['java', java_class]
    proc = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
    stdout,stderr = proc.communicate(stdin)
    return stdout

compile_java('helloworld.java')
java_output = execute_java('helloworld.java', 'args')