Python:如何通过Python脚本执行jar文件

发布于 2024-12-03 22:26:46 字数 873 浏览 3 评论 0原文

我一直在寻找如何通过 python 执行 java jar 文件的答案,并查看了:

Execute 。来自 Python 的 jar

如何让我的 python(版本 2.5)脚本在文件夹内而不是从命令行运行 jar 文件?

如何直接运行Python Egg文件而不安装它们?

我尝试执行以下操作(我的 jar 和 python 文件都在同一目录中):

import os

if __name__ == "__main__":
    os.system("java -jar Blender.jar")

并且

import subprocess

subprocess.call(['(path)Blender.jar'])

都没有工作。所以,我想我应该使用 Jython 来代替,但我认为必须有一种更简单的方法来通过 python 执行 jar 文件。

你知道我可能做错了什么吗?或者,还有其他网站可以让我更多地研究我的问题吗?

I have been looking for an answer for how to execute a java jar file through python and after looking at:

Execute .jar from Python

How can I get my python (version 2.5) script to run a jar file inside a folder instead of from command line?

How to run Python egg files directly without installing them?

I tried to do the following (both my jar and python file are in the same directory):

import os

if __name__ == "__main__":
    os.system("java -jar Blender.jar")

and

import subprocess

subprocess.call(['(path)Blender.jar'])

Neither have worked. So, I was thinking that I should use Jython instead, but I think there must a be an easier way to execute jar files through python.

Do you have any idea what I may do wrong? Or, is there any other site that I study more about my problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拥醉 2024-12-10 22:26:46

我会这样使用子进程:

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

但是,如果您有正确配置的 /proc/sys/fs/binfmt_misc/jar 您应该能够直接运行 jar,正如您所写的。

那么,这正是您遇到的错误?
请将您从失败的执行中获得的所有输出发布到某处。

I would use subprocess this way:

import subprocess
subprocess.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.

我做我的改变 2024-12-10 22:26:46

这总是对我有用:

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 ret

args = ['myJarFile.jar', 'arg1', 'arg2', 'argN'] # Any number of args to be passed to the jar file

result = jarWrapper(*args)

print result

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 ret

args = ['myJarFile.jar', 'arg1', 'arg2', 'argN'] # Any number of args to be passed to the jar file

result = jarWrapper(*args)

print result
风轻花落早 2024-12-10 22:26:46

我使用以下方式执行tika jar来提取word文档的内容。它有效,我也得到了输出。我尝试运行的命令是 "java -jar tika-app-1.24.1.jar -t 42250_EN_Upload.docx"

from subprocess import PIPE, Popen
process = 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'))

这里我得到了元组结果,因此 "result[0 ]”。该字符串也是二进制格式(b 字符串)。要将其转换为普通字符串,我们需要使用“utf-8”进行解码。

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, Popen
process = 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'.

伪装你 2024-12-10 22:26:46

使用参数:使用 Closure Compiler 的具体示例 (https://developers.google.com/closure/ )来自 python

import os
import re
src = test.js
os.execlp("java", 'blablabla', "-jar", './closure_compiler.jar', '--js', src, '--js_output_file',  '{}'.format(re.sub('.js

(另请参阅此处 当使用os.execlp,为什么`python`需要`python`作为argv[0]

, '.comp.js', src)))

(另请参阅此处 当使用os.execlp,为什么`python`需要`python`作为argv[0]

With args: concrete example using Closure Compiler (https://developers.google.com/closure/) from python

import os
import re
src = test.js
os.execlp("java", 'blablabla', "-jar", './closure_compiler.jar', '--js', src, '--js_output_file',  '{}'.format(re.sub('.js

(also see here When using os.execlp, why `python` needs `python` as argv[0])

, '.comp.js', src)))

(also see here When using os.execlp, why `python` needs `python` as argv[0])

不必在意 2024-12-10 22:26:46

使用 os.system() 怎么样:

os.system('java -jar blabla...')

os.system(command)
在子 shell 中执行命令(字符串)。这是通过调用标准 C 函数 system() 来实现的,并且具有相同的限制。对 sys.stdin 等的更改不会反映在执行命令的环境中。

How about using os.system() like:

os.system('java -jar blabla...')

os.system(command)
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文