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

adragomir
  • 457
  • 4
  • 16
  • 33

1 Answers1

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.

greschd
  • 606
  • 8
  • 19
  • 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](http://stackoverflow.com/a/20981435/4279); it should work. – jfs Oct 21 '14 at 02:28