4

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?

frogatto
  • 28,539
  • 11
  • 83
  • 129
Viraj Kamath
  • 71
  • 1
  • 4
  • 8

1 Answers1

8

You should use communicate instead of stdout.read. The argument of communicate is the input to the subprocess. Also, it's a bad idea to pass shell=True when you don't actually want a shell to execute your command. Instead, pass the arguments as a list:

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)
    print ('This was "' + stdout + '"')

compile_java('Hi.java')
execute_java('Hi.java', 'Jon')
phihag
  • 278,196
  • 72
  • 453
  • 469
  • thanks for the solution.. But I dont want to pass the string as a parameter to java program i.e execute_java('Hi.java', 'Jon') nor i want to pass a parameter as stdout,stderr = proc.communicate("Jon"). I want that the java program should wait after asking me the input. And when i entere the input, It must continue its execution.Pls help.. – Viraj Kamath Mar 07 '12 at 03:21
  • Umm, the parameter-to-communicate variant does that - it writes `"Jon"` to the Java program's standard input channel, and waits for the Java Program to execute. If you want to interactively deal with the program (i.e. send multiple inputs depending on the output of the Java program), you can use `proc.stdin` and `proc.stdout`. If that's the case, please ask a new question. In what way does the Python program in this answer **not** solve your problem? – phihag Mar 07 '12 at 10:43
  • I have made use of proc.stdin and proc.stdout and hence my python program is.. 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() but due to this, the output is "Enter any string" and when i enter any string say "Jon", then it again prints as "Enter any string This is Jon".. i.e. it is printing the output two times. – Viraj Kamath Mar 07 '12 at 15:58
  • Well, you're executing the program twice, for no good reason. Remove the `input = subprocess.Popen(cmd,stdin=PIPE)` line. – phihag Mar 07 '12 at 16:33
  • if I remove the line input = subprocess.Popen(cmd,stdin=PIPE) , it just provides me all the output of javaprogram.. it never executes any input of java program.. and if i pass stdin=PIPE as an argument to proc=subprocess.Popen above statement then it not even gives any output of java.. – Viraj Kamath Mar 07 '12 at 17:54
  • Well, you have to actually write something to stdin. Again, what's wrong with the code in this answer? – phihag Mar 07 '12 at 17:58
  • I dont know wats wrong but wen I write the code as proc = subprocess.Popen(cmd,stdout=PIPE,stdin=PIPE,stderr=STDOUT) print proc.stdout.read() it does not give me any output – Viraj Kamath Mar 07 '12 at 18:02
  • No offense, but if you write significantly different code than the one in this answer, why do you expect it to get the same result? Please copy the code from this answer, check that it works, and *(only) if it does not*, comment on how exactly it is failing or lacking. If you don't want to use `communicate` for some reason, please *post* the reason. – phihag Mar 07 '12 at 18:05
  • No.. I dont have any problem in using communicate.. But using communicate, I have to pass the input as a parameter and then it displays the output as the example given by you. But i want that my java program must ask the input it must wait, when user will enter the input, it must first get stored in python variable and then this variable will provide the input to java program and thus java program will continue its execution.. I want the flow of my program in this way and I didnt get any solution for this using communicate command.. – Viraj Kamath Mar 07 '12 at 18:13
  • What exactly do you mean by `it must wait`? As soon as the Java program calls `t.nextLine`, it rescinds control to the operating system, and waits, if only for a couple of nanoseconds. I strongly suggest that you ask a new stackoverflow question where you describe **in detail** what behavior you want. – phihag Mar 07 '12 at 18:17
  • Do u know pexpect of python which work on linux? where child = pexpect.spawn("java Test") and then child.expect(".*") commands can perform line by line execution of java program.. the same way, I wanted to do in windows either using communicate or stdin-stdout.. I have already explained you the flow of program i wanted..I think you might not have executed it and hence u couldnt get my problem. sorry for your inconvenience.. – Viraj Kamath Mar 07 '12 at 18:29
  • What if I have multiple inputs ? – Anurag-Sharma Oct 17 '15 at 16:33