4

What is the nicest way to read the output (i.e. via System.out.println) of a Java app which is called from Python with

subprocess.Popen("java MyClass", shell=True)

without writing and reading a file? (Using Jython etc is not a possible solution)

1
  • sidenote: prefer the syntax given by S. Mark below when using subprocess.Popen (i.e. the list for the first argument, and the default value for shell (False). See logilab.org/blogentry/20469 for more on this. Commented Mar 5, 2010 at 17:12

2 Answers 2

5
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

Use Popen.communicate() to perform a blocking read until popened process terminates.
3

I just found the solution:

p = subprocess.Popen("java MyClass",
          shell=True,
          stdout=subprocess.PIPE)
output, errors = p.communicate()

S.Mark's is fine too!

1 Comment

I was unable to call javac src/*.java by passing a list to Popen due to the wildcard, not sure why. Giving a string and using communicate() works

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.