0

I am new to python subprocess and i do not understand the documentation.

I am trying to run a jar file within my python program and pipe the output back to python.

sys.stdout.write("haha")
subprocess.Popen(['java', 'myjarfile.jar'], stdin= subprocess.PIPE,
 stdout = subprocess.PIPE)
ans = raw_input("")

However, this does not seem to work

Terry Jan Reedy
19.3k3 gold badges44 silver badges57 bronze badges
asked Mar 2, 2015 at 3:40
2
  • What are you trying to do? What do you expect sys.stdout.write() and raw_input() to achieve in this case? Commented Mar 2, 2015 at 9:31
  • wording and linewrap Commented Mar 6, 2015 at 21:24

1 Answer 1

1

To pass b'haha' bytestring as an input to the java child process and to get its stdout as ans bytestring:

#!/usr/bin/env python3
from subprocess import check_output
ans = check_output(['java', '-jar', 'myjarfile.jar'], input=b'haha')

input parameter is supported only since Python 3.4, you could use .communicate() on older Python versions:

#!/usr/bin/env python
from subprocess import Popen, PIPE
p = Popen(['java', '-jar', 'myjarfile.jar'], stdin=PIPE, stdout=PIPE)
ans = p.communicate(b'haha')[0]
if p.returncode != 0:
 raise Error
answered Mar 10, 2015 at 16:28
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.