Thomas
Thomas

Reputation: 613

Python 3 - Using subprocess module to execute java command line prompt

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.

Upvotes: 0

Views: 2977

Answers (1)

Thomas
Thomas

Reputation: 613

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')

Upvotes: 1

Related Questions