-3

I have a java program that i run with the following command

java -jar <program_name>.jar --<some_parameter> <some_filename>.csv

Within my python script I create the <some_filename>.csv. Then, I would like to execute the java program and use the program's stdout output in my python script.

Is there an easy way to do so?

Joe
  • 1

2 Answers2

1

Try with subprocess:

import subprocess
result = subprocess.run([COMMAND LIST], stdout=subprocess.PIPE)
print(result.stdout)

[COMMAND LIST] is a string list of the words in the command separated by space

Wasif
  • 14,755
  • 3
  • 14
  • 34
0

Try with this:

import subprocess

p = subprocess.Popen(["java", "-jar <program_name>.jar --<some_parameter> <some_filename>.csv"],shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if not out:
   print(err.rstrip().decode())
else:
   print(out.rstrip().decode())
SAL
  • 547
  • 2
  • 8
  • 25