Running Jar Files from Python

I want to create a program that can execute jar files and print all jar files in my python program, but without using the Windows command line, I searched all over the network but didn't figure anything out how to do this.

My program is a Minecraft server shell, and I want it to run the server.jar file and instead of running it on the Windows command line, I want it to run inside the Python shell.

Any ideas?

+6
source share
1 answer

First you need to run the program. Convenient function for this:

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

It will return iterability with all output lines.
And you can access the lines and print with

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

add import subprocess as well, since run_command uses subprocess .

+7
source

Source: https://habr.com/ru/post/947920/


All Articles