5

I am a python3 beginner. I am trying to get the java version with a python3 script. After I checked the docs, I saw that subprocess.check_output might be what I need.

output = subprocess.check_output(["java", "-version"])
print("Output is {}".format(output))

The problem is the output I am getting is

Output is b''

Why am I not getting the correct string that I get with bash?

Thanks

1 Answer 1

7

For some reason your output lands in the stderr. You can pipe that to the return value like this:

output = subprocess.check_output(["java", "-version"], stderr=subprocess.STDOUT)

If somebody knows why it goes to stderr, I'd be happy to hear it. ["python", "--version"], for example, works as expected.

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

1 Comment

I can confirm: java -version command prints to stderr on my system. man java doesn't specify where the output should go. Your code covers both stdout/stderr therefore unless java -version prints directly to terminal; it should work.

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.