Carter
Carter

Reputation: 439

Running a jar with python and passing input/output

So I've looked a bit into the subprocess library, and I think that it is the right library to use, but I can't seem to find out how one would go about passing input to the program during runtime. I want to run a jar using a python script and analyze the output of that jar file while it is running (for an indefinite amount of time). Then, based on that output, I want to be able to pass back input to the jar file (which is accepting input).

Upvotes: 1

Views: 2552

Answers (1)

Grim
Grim

Reputation: 1638

You will need to use the subprocess.Popen constructor, passing as stdin and stdout arguments either:

  • a file-like object of your choice, which you can then read and write in a loop during the program execution
  • the special value subprocess.PIPE, which allows then to access these streams through the returned Popen object as properties stdin and stdout. Liek the first case, you'll need to run a loop reading from stdout and writing to stdin.

There are some nice examples at http://sharats.me/the-ever-useful-and-neat-subprocess-module.html

Upvotes: 1

Related Questions