我可以让jar执行python脚本吗? [英] Can I make a jar execute a python script?

查看:133
本文介绍了我可以让jar执行python脚本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在jar中调用脚本而不提取它?

Is there some way to call a script within a jar without extracting it?

推荐答案

Python将解释一个名为 __main__.py存储在zip文件中,如果使用one作为参数调用。由于jar文件是zip文件,所以你要做的就是将你的Python脚本命名为__main__.py,或者创建一个导入主脚本的__main__.py脚本,并从Java调用Python解释器作为外部进程,将.jar文件路径作为唯一参数传递。 (从Python脚本中导入其他Python模块就像.jar文件所在的目录结构一样)

Python will interpret a file named "__main__.py" stored inside a zip file, if called with one as a parameter. Since jar files are zips, all you have to do is name your Python script as "__main__.py", or create a "__main__.py" script that imports your main script, and from Java, invoke the Python interpreter as an external process, passing the .jar file path as its sole argument. (Importing other Python modules from within the Python script will work as if the .jar file where a directory structure)

然后,您可以通过管道(stdin)与Python进程通信/ stdout)或使用一些客户端/服务器方法(xmlrpc,Unix命名管道等等)

You can then communicate with the Python process via pipe (stdin/stdout) or using some client/server approach (xmlrpc, Unix named pipes, etc...)

另一种选择是使用Jython - 基于JVM的Python解释器,这样你就可以直接从你的java代码调用Python代码 -
有几个选项可以将Jython代码集成到java程序中,这里描述了一些女巫:
http://jythonpodcast.hostjava.net/jythonbook/en/1.0/JythonAndJavaIntegration。 html#using-jython-within-java-applications

Another option would be to use Jython - the JVM based Python interpreter, so that you can call Python code directly from your java code - there are several options to integrate Jython code into java programs, some of witch are described here: http://jythonpodcast.hostjava.net/jythonbook/en/1.0/JythonAndJavaIntegration.html#using-jython-within-java-applications

这篇关于我可以让jar执行python脚本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆