Python: How can I execute a jar file through a python script Python: How can I execute a jar file through a python script python python

Python: How can I execute a jar file through a python script


I would use subprocess this way:

import subprocesssubprocess.call(['java', '-jar', 'Blender.jar'])

But, if you have a properly configured /proc/sys/fs/binfmt_misc/jar you should be able to run the jar directly, as you wrote.

So, which is exactly the error you are getting?Please post somewhere all the output you are getting from the failed execution.


This always works for me:

from subprocess import *def jarWrapper(*args):    process = Popen(['java', '-jar']+list(args), stdout=PIPE, stderr=PIPE)    ret = []    while process.poll() is None:        line = process.stdout.readline()        if line != '' and line.endswith('\n'):            ret.append(line[:-1])    stdout, stderr = process.communicate()    ret += stdout.split('\n')    if stderr != '':        ret += stderr.split('\n')    ret.remove('')    return retargs = ['myJarFile.jar', 'arg1', 'arg2', 'argN'] # Any number of args to be passed to the jar fileresult = jarWrapper(*args)print result


I used the following way to execute tika jar to extract the content of a word document. It worked and I got the output also. The command I'm trying to run is "java -jar tika-app-1.24.1.jar -t 42250_EN_Upload.docx"

from subprocess import PIPE, Popenprocess = Popen(['java', '-jar', 'tika-app-1.24.1.jar', '-t', '42250_EN_Upload.docx'], stdout=PIPE, stderr=PIPE)result = process.communicate()print(result[0].decode('utf-8'))

Here I got result as tuple, hence "result[0]". Also the string was in binary format (b-string). To convert it into normal string we need to decode with 'utf-8'.