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?

2
  • 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! Commented Jun 23, 2013 at 4:24
  • 1
    There is some more info now, Hope that helps explaining what Im doing. Commented Jun 23, 2013 at 4:33

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

I get an error saying b'Error: Could not find or load main class craftbukkit.jar\r\n', so what do i do.
the output is originally from the jvm, so i suppose your java code has some bug.
well it is just a minecraft server jar file
oops, the command should be java -jar jarfile.jar, sorry. Correcting error now.
Ok now it is working but the java console is open so is there a wayto hide this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.