Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Jar files from Python

Tags:

java

python

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?

like image 534
Dan Alexander Avatar asked Jun 23 '13 04:06

Dan Alexander


1 Answers

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.

like image 67
svineet Avatar answered Oct 12 '22 00:10

svineet