-2

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?

Chris
  • 44,602
  • 16
  • 137
  • 156
Viraj Kamath
  • 71
  • 1
  • 4
  • 8

1 Answers1

2

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()
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • I have also tried this solution.. But the output is "Tested SUccessfully Enter the string" and then "java.util.noSuchElementException: No line found" is coming.. – Viraj Kamath Mar 06 '12 at 01:37
  • Oops, remove the `stdin = PIPE` from the call to `subprocess.Popen`. – unutbu Mar 06 '12 at 02:31