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?

1 Answer 1

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')
Sign up to request clarification or add additional context in comments.

12 Comments

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..
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?
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.
Well, you're executing the program twice, for no good reason. Remove the input = subprocess.Popen(cmd,stdin=PIPE) line.
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..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.