且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以让jar执行python脚本吗?

更新时间:2023-12-05 14:22:22

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