6

I want to make a program that can execute jar files and print whatever the jar file is doing in my python program but without using the windows command line, I have searched all over the web but nothing is coming up with how to do this.

My program is a Minecraft server wrapper and I want it to run the server.jar file and instead of running it within the windows command prompt I want it to run inside the Python shell.

Any ideas?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Dan Alexander
  • 2,004
  • 6
  • 24
  • 34
  • Hi Daniel, what is the purpose of wanting to listen in on what other applications are doing? Is it the logs you want to listen to or something else? Network traffic? Something else? Please provide some more detail to paint a healthy picture of your problem. Hope this helps! – jamesmortensen Jun 23 '13 at 04:24
  • 1
    There is some more info now, Hope that helps explaining what Im doing. – Dan Alexander Jun 23 '13 at 04:33

1 Answers1

10

First you have to execute the program. A handy function for doing so:

def run_command(command):
    p = subprocess.Popen(command,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT)
    return iter(p.stdout.readline, b'')

It will return an iterable with all the lines of output.
And you can access the lines and print using

for output_line in run_command('java -jar jarfile.jar'):
    print(output_line)

add also import subprocess, as run_command uses subprocess.

Ry-
  • 218,210
  • 55
  • 464
  • 476
svineet
  • 1,859
  • 1
  • 17
  • 28