Consider my python program as input.py
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):
java_class,ext = os.path.splitext(java_file)
cmd = ['java', java_class]
proc = subprocess.Popen(cmd,stdout=PIPE,stderr=STDOUT)
input=subprocess.Popen(cmd,stdin=PIPE)
print proc.stdout.read()
Java file I am using is Hi.java
import java.util.*;
class Hi
{
public static void main(String args[])
{
Scanner t=new Scanner(System.in);
System.out.println("Enter any string");
String str=t.nextLine();
System.out.println("This is "+str);
int a=5;
System.out.println(a);
}
}
When I call input.execute_java(Hi.hava)
, the output is "Enter the string"
and when I enter the string say "Jon"
, then again it prints the output as "Enter the string This is Jon" i.e. it is providing the entire output two times. Maybe one output due to the python code input=subprocess.Popen(cmd,stdin=PIPE)
and second output due to the code print proc.stdout.read()
I want it to get printed only once. What should I do? I want my python variable to receive all the output of java program and using that variable I will display the output on the screen. Also if there is an input to java program, I want that user to enter the input which will be stored in my python variable and using this variable, I want to pass the input to java program. What should I do?