Python:如何通过Python脚本执行jar文件
我一直在寻找如何通过 python 执行 java jar 文件的答案,并查看了:
如何让我的 python(版本 2.5)脚本在文件夹内而不是从命令行运行 jar 文件?
我尝试执行以下操作(我的 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会这样使用子进程:
但是,如果您有正确配置的
/proc/sys/fs/binfmt_misc/jar
您应该能够直接运行 jar,正如您所写的。那么,这正是您遇到的错误?
请将您从失败的执行中获得的所有输出发布到某处。
I would use subprocess this way:
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:
我使用以下方式执行tika jar来提取word文档的内容。它有效,我也得到了输出。我尝试运行的命令是
"java -jar tika-app-1.24.1.jar -t 42250_EN_Upload.docx"
这里我得到了元组结果,因此
"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"
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'.使用参数:使用 Closure Compiler 的具体示例 (https://developers.google.com/closure/ )来自 python
(另请参阅此处 当使用os.execlp,为什么`python`需要`python`作为argv[0])
With args: concrete example using Closure Compiler (https://developers.google.com/closure/) from python
(also see here When using os.execlp, why `python` needs `python` as argv[0])
使用 os.system() 怎么样:
os.system(command)
在子 shell 中执行命令(字符串)。这是通过调用标准 C 函数 system() 来实现的,并且具有相同的限制。对 sys.stdin 等的更改不会反映在执行命令的环境中。
How about using os.system() like:
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.