0

I have a java program on my computer that has a command line api. If navigate to the appropriate folder in a command prompt, I can enter the command

java -cp p2.jar propokertools.cli.RunPQL

to make various poker calculations from a command prompt. I would like to be able to interact with this program through Python.

I've tried:

p = subprocess.check_output(['java', '-cp', 'p2.jar propokertools.cli.RunPQL'], cwd='C:\\Program Files\\PPTOddsOracle\\ui_jar')

but get this error:

File "C:\Python34\lib\subprocess.py", line 620, in check_output raise CalledProcessError(retcode, process.args, output=output) subprocess.CalledProcessError: Command '['java', '-cp p2.jar propokertools.cli.RunPQL']' returned non-zero exit status 1

I decided to be less ambitious and just try to figure out how to get Python to return my java version, but

p = subprocess.check_output(['java', '-version'])

and

p = subprocess.Popen(["java", "-version"], stdout=subprocess.PIPE)
p.stdout.read()

both returned empty byte strings, so I think I'm doing something very wrong. Any help would be greatly appreciated.

3
  • 1
    java -version writes to stderr, not stdout. Commented Mar 29, 2015 at 16:42
  • Heyyy... subprocess.Popen(["java", "-version"], stderr=subprocess.PIPE).communicate() does indeed return my Java version! Thanks! Commented Mar 29, 2015 at 16:57
  • 1
    to merge stdout/stderr: outtput = subprocess.check_output(['java', '-version'], stderr=subprocess.STDOUT) Commented Mar 29, 2015 at 21:50

1 Answer 1

1

I figured it out. I was missing that 'p2.jar propokertools.cli.RunPQL' needed to be split up into separate arguments.

p = subprocess.check_output(['java', '-cp', 'p2.jar', 'propokertools.cli.RunPQL'], cwd='C:\\Program Files\\PPTOddsOracle\\ui_jar')
Sign up to request clarification or add additional context in comments.

3 Comments

I have found that you need separate arguments in a list if you have shell=False (the default).
You're right. This also works: subprocess.check_output('java -cp p2.jar propokertools.cli.RunPQL', cwd='C:\\Program Files\\PPTOddsOracle\\ui_jar',shell=False)
@cdarke: you don't need it on Windows. Though you could do it for portability.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.