title: How to execute java program using python considering inputs and outputs both
body: <p>Consider my python program as input.py</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>Java file I am using is Hi.java</p>
  
  <pre><code>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);
         }
  }
  </code></pre>
  
  <p>When I call <code>input.execute_java(Hi.hava)</code>, the output is <code>"Enter the string"</code> and when I enter the string say <code>"Jon"</code>, 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 <code>input=subprocess.Popen(cmd,stdin=PIPE)</code> and second output due to the code <code>print proc.stdout.read()</code>
  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?</p>