Communicate with Java program from Python
Question: My Python progam is "check.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,stdin-PIPE,stderr=STDOUT)
        stdout,stderr = proc.communicate("viraj")
        print stdout
    

My Java program is "Test.java"

    
    
    import java.util.*;
    class Test
    {
        public static void main(String args[])
        {
            System.out.println("Tested Successfully");
            Scanner t=new Scanner(System.in);
            System.out.println("Enter the string");
            String str=t.nextLine();
                System.out.println("Hello "+str);
           }
    }
    

Here, in `check.py`, I am passing a parameter `viraj` as
`proc.communicate("viraj")` and hence my output is

> Tested Successfully  
>  Enter the string  
>  Hello viraj

However, I don't want to pass the parameter. Instead I want the output as

> Tested Successfully  
>  Enter the String

and then after I enter the string as `viraj`, it should get printed as `Hello
viraj` How can I do this?


Answer: To interact with the java program, simply call `communicate()` with no
arguments.

    
    
    def execute_java(java_file):
        java_class,ext = os.path.splitext(java_file)
        cmd = ['java', java_class]
        proc = subprocess.Popen(cmd)
        proc.communicate()
    

