0

I am running a Python script that runs a Java class that requires nondeterministic user input:

import sys
import subprocess
from subprocess import Popen, PIPE

p = Popen("java myclass", shell=True, stdout=subprocess.PIPE)

while True:
    inline = p.stdout.readline()
    if not inline:
        break

sys.stdout.write(inline)
sys.stdout.flush()

When output is displayed using System.out.print(), the output is displayed properly. However, when a prompt for user input is printed with System.out.print(), the output is not displayed. I tried switching p.stdout.readline() to p.stdout.read(), but then no output is displayed. Is there a way to display an input prompt that does not have a carriage return?

EDIT

For example, if the Java class contains the following:

System.out.println("Message 1);
System.out.println("Message 2);
System.out.print("Enter a number:")

Running the pure Java code would display all 3 lines.

The code running through Python would only display the following:

Message 1
Message 2
2
  • You can use strip to remove the carriage return : inline = p.stdout.readline().strip() Commented Apr 25, 2013 at 20:26
  • I am confused. All System.out prints are done to STDOUT, so reading it should be no different. However, a System.in is reading from STDIN, so you would have to write out the "user input" with the appropriate combination of keys to indicate the input had stopped. Commented Apr 25, 2013 at 20:28

1 Answer 1

2
p1 = subprocess.Popen(["/usr/bin/java", "MyClass"], stdout=subprocess.PIPE)
print p1.stdout.read() 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was able to get the prompt using this:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.