-1

I have jar file when executed will print out the output on the terminal, Im not sure how can i pass the jar file output as a json file.

The code below just print the jar file output on the terminal

subprocess.Popen(['java', '-jar', '/home/myfolder/collect.jar'])

I'm thinking of below but no idea to start with...

with open('collect.json', 'w') as fp:
    xxxxxxxxxxx

Hope someone could advise further. Thank you.

2

2 Answers 2

0

This should do the trick. If you look at the subprocess documentation you can see that check_output runs a command with arguments and return its output as a byte string.

import multiprocessing as mp
import subprocess

command = "java -jar /home/myfolder/collect.jar"

def runCommand(q):
    commandOutput = subprocess.check_output(command.split()).decode("utf-8")
    q.put(commandOutput)

q = mp.Queue()
commandProcess = mp.Process(target=runCommand, args=(q, ))
commandProcess.start()
output = q.get()
print(output)
with open('collect.json', w) as fp: 
    fp.write(output)

Didn't run the code, but should work.

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

4 Comments

Hi...Im getting error ImportError: No module named mutiprocessing.my pip list does have mutiprocess install
Sorry...typo error of multiprocessing..i run again the code... anyway...the code run but the json file empty...no data on it...
@chenoi Try the code above again, i fotgot to actually write the file (see fp.write(output))
It works...thank you so much.... i spot the missing pieces earlier...thank you for your attention and support...
0

You can try something like this

with open('collect.json','w') as fp :
  subprocess.Popen('java -jar .//home/myfolder/collect.jar',stdout=fp).wait()

for further information, see this question How to get the output from .jar execution in python codes?

I hope this helped.

2 Comments

hi....this getting error raise TypeError("bufsize must be an integer") TypeError: bufsize must be an integer
I add [] as below subprocess.Popen(['java -jar .//home/myfolder/collect.jar'],stdout=fp).wait()

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.