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?
-
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– jamesmortensen2013年06月23日 04:24:42 +00:00Commented Jun 23, 2013 at 4:24
-
1There is some more info now, Hope that helps explaining what Im doing.Dan Alexander– Dan Alexander2013年06月23日 04:33:01 +00:00Commented Jun 23, 2013 at 4:33
1 Answer 1
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.