recieve output from java python 1

recieve output from java python

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')

Here is what the above code is Doing:
1. Compile the Java code using javac
2. Execute the Java code using java
3. Pass the input to the Java code using stdin
4. Capture the output of the Java code using stdout

Similar Posts