python与java互相调用

总结结论

简单给大家做个总结

  • 如果想用java调用python,推荐jep
  • 如果想用python调用java,推荐jpype

其他项目的活跃度要么不高,要么就是太落后python的版本,或者不兼容python的生态。

还有个可以期待的项目 Graalvm,由Oracle维护,但是对Python的已有生态不太兼容,可以观望。

jpype对类似方案的解读

Alternatives

JPype is not the only Python module of its kind that acts as a bridge to Java. Depending on your programming requirements, one of the alternatives may be a better fit. Specifically JPype is designed for clarity and high levels of integration between the Python and Java virtual machine. As such it makes use of JNI and thus inherits all of the benefits and limitations that JNI imposes. With JPype, both virtual machines are running in the same process and are sharing the same memory space and threads. JPype can thus intermingle Python and Java threads and exchange memory quickly. But by extension you can’t start and stop the JVM machine but instead must keep both machines throughout the lifespan of the program. High integration means tightly coupled and thus it embodies the musketeers motto. If Python crashes, so does Java as they only have one process to live in.

JPype 并不是唯一一个充当 Java 桥梁的同类 Python 模块。根据您的编程要求,其中一种选择可能更合适。具体来说,JPype 旨在实现 Python 和 Java 虚拟机之间的清晰和高度集成。因此,它利用了 JNI,因此继承了 JNI 强加的所有优点和限制。使用 JPype,两个虚拟机都在同一个进程中运行,并且共享相同的内存空间和线程。因此,JPype 可以混合 Python 和 Java 线程并快速交换内存。但是通过扩展,您不能启动和停止 JVM 机器,而是必须在程序的整个生命周期中保持两台机器。高度集成意味着紧密耦合,因此它体现了火枪手的座右铭。如果 Python 崩溃,

A few alternatives with different philosophies and limitations are given in the following section. Please take my review comments with the appropriate grain of salt. When I was tasked with finding a replacement for Matlab Java integration for our project test bench, I evaluated a number of alternatives Python bridge codes. I selected JPype primarily because it presented the most integrated API and documentation which would be suitable for getting physicists up to speed quickly. Thus your criteria may yield a different selection. JPype’s underlying technology was underwhelming so I have had the pleasure of many hours reworking stuff under the hood.

For more details on what you can’t do with JPype, please see Limitations.

以下部分给出了一些具有不同理念和局限性的替代方案。请用适当的盐粒来接受我的评论意见。当我的任务是为我们的项目测试台寻找 Matlab Java 集成的替代品时,我评估了许多替代 Python 桥代码。我选择 JPype 主要是因为它提供了最集成的 API 和文档,适合让物理学家快速上手。因此,您的标准可能会产生不同的选择。JPype 的底层技术并没有给人留下深刻印象,所以我有幸在引擎盖下进行了许多小时的返工。

有关 JPype 不能做什么的更多详细信息,请参阅限制

Jython

Jython is a reimplementation of Python in Java. As a result it has much lower costs to share data structures between Java and Python and potentially much higher level of integration. Noted downsides of Jython are that it has lagged well behind the state of the art in Python; it has a limited selection of modules that can be used; and the Python object thrashing is not particularly well fit in Java virtual machine leading to some known performance issues.

Jython 是用 Java 重新实现的 Python。因此,在 Java 和 Python 之间共享数据结构的成本要低得多,而且集成度可能要高得多。Jython 的显着缺点是它远远落后于 Python 的最新技术。它可以使用的模块选择有限;并且 Python 对象抖动并不是特别适合 Java 虚拟机,这会导致一些已知的性能问题。

Py4J

Py4J uses a remote tunnel to operate the JVM. This has the advantage that the remote JVM does not share the same memory space and multiple JVMs can be controlled. It provides a fairly general API, but the overall integration to Python is as one would expect when operating a remote channel operating more like an RPC front-end. It seems well documented and capable. Although I haven’t done benchmarking, a remote access JVM will have a transfer penalty when moving data.

Py4J 使用远程隧道来操作 JVM。这样做的好处是远程JVM不共享相同的内存空间,可以控制多个JVM。它提供了一个相当通用的 API,但与 Python 的整体集成正如人们在操作更像 RPC 前端的远程通道时所期望的那样。它似乎有据可查且功能强大。虽然我没有做过基准测试,但远程访问 JVM 在移动数据时会产生传输损失。

Jep

Jep stands for Java embedded Python. It is a mirror image of JPype. Rather that focusing on accessing Java from within Python, this project is geared towards allowing Java to access Python as sub-interpreter. The syntax for accessing Java resources from within the embedded Python is quite similar with support for imports. Notable downsides are that although Python supports multiple interpreters many Python modules do not, thus some of the advantages of the use of Python many be hard to realize. In addition, the documentation is a bit underwhelming thus it is difficult to see how capable it is from the limited examples.

Jep 代表 Java 嵌入式 Python。它是 JPype 的镜像。该项目不是专注于从 Python 中访问 Java,而是旨在允许 Java 作为子解释器访问 Python。从嵌入式 Python 中访问 Java 资源的语法与对导入的支持非常相似。显着的缺点是尽管 Python 支持多个解释器,但许多 Python 模块不支持,因此使用 Python 的一些优点很难实现。此外,文档有点令人印象深刻,因此很难从有限的示例中看出它的能力。

PyJnius

PyJnius is another Python to Java only bridge. Syntax is somewhat similar to JPype in that classes can be loaded in and then have mostly Java native syntax. Like JPype, it provides an ability to customize Java classes so that they appear more like native classes. PyJnius seems to be focused on Android. It is written using Cython .pxi files for speed. It does not include a method to represent primitive arrays, thus Python list must be converted whenever an array needs to be passed as an argument or a return. This seems pretty prohibitive for scientific code. PyJnius appears is still in active development.

PyJnius 是另一个 Python 到 Java 的唯一桥梁。语法有点类似于 JPype,因为可以加载类,然后大部分是 Java 原生语法。与 JPype 一样,它提供了自定义 Java 类的能力,使它们看起来更像原生类。PyJnius 似乎专注于 Android。它是使用 Cython .pxi 文件编写的以提高速度。它不包括表示原始数组的方法,因此每当需要将数组作为参数或返回值传递时,都必须转换 Python 列表。这对于科学代码来说似乎是非常令人望而却步的。PyJnius 似乎仍在积极开发中。

Javabridge

Javabridge is direct low level JNI control from Python. The integration level is quite low on this, but it does serve the purpose of providing the JNI API to Python rather than attempting to wrap Java in a Python skin. The downside being of course you would really have to know a lot of JNI to make effective use of it.

Javabridge 是来自 Python 的直接低级 JNI 控件。这方面的集成级别相当低,但它确实起到了向 Python 提供 JNI API 的目的,而不是试图将 Java 包装在 Python 皮肤中。缺点当然是你真的需要了解很多 JNI 才能有效地使用它。

jpy

This is the most similar package to JPype in terms of project goals. They have achieved more capabilities in terms of a Java from Python than JPype which does not support any reverse capabilities. It is currently unclear if this project is still active as the most recent release is dated 2014. The integration level with Python is fairly low currently though what they do provide is a similar API to JPype.

就项目目标而言,这是与 JPype 最相似的包。他们从 Python 的 Java 方面获得了比不支持任何反向功能的 JPype 更多的功能。目前还不清楚这个项目是否仍然活跃,因为最新版本是 2014 年。目前与 Python 的集成水平相当低,尽管它们提供的是与 JPype 类似的 API。

JCC

JCC is a C++ code generator that produces a C++ object interface wrapping a Java library via Java’s Native Interface (JNI). JCC also generates C++ wrappers that conform to Python’s C type system making the instances of Java classes directly available to a Python interpreter. This may be handy if your goal is not to make use of all of Java but rather have a specific library exposed to Python.

JCC 是一个 C++ 代码生成器,它通过 Java 的本机接口 (JNI) 生成一个包含 Java 库的 C++ 对象接口。JCC 还生成符合 Python 的 C 类型系统的 C++ 包装器,使 Java 类的实例直接可供 Python 解释器使用。如果您的目标不是使用所有 Java,而是将特定库暴露给 Python,这可能会很方便。

VOC https://beeware.org/project/projects/bridges/voc/_

A transpiler that converts Python bytecode into Java bytecode part of the BeeWare project. This may be useful if getting a smallish piece of Python code hooked into Java. It currently list itself as early development. This is more in the reverse direction as its goals are making Python code available in Java rather providing interaction between the two.

一个将 Python 字节码转换为 BeeWare 项目的 Java 字节码部分的转译器。如果将一小段 Python 代码挂接到 Java 中,这可能会很有用。它目前将自己列为早期开发。这更像是相反的方向,因为它的目标是使 Python 代码在 Java 中可用,而不是提供两者之间的交互。

p2j

This lists itself as “A (restricted) python to java source translator”. Appears to try to convert Python code into Java. Has not been actively maintained since 2013. Like VOC this is primilarly for code translation rather that bridging.

这将自己列为“A(受限)python 到 java 源代码转换器”。似乎试图将 Python 代码转换为 Java。自 2013 年以来一直没有积极维护。像 VOC 一样,这主要是为了代码翻译而不是桥接。

jep对jpype的解释

如果想用python访问java,推荐jpype

  • Python
    • Sinces Jep embeds CPython, it can run Python code just like other Python interpreters. But it cannot be launched from a Python process (we may add this in the future). If you want access to Java from a Python process, consider the following projects:

相关链接

https://talvi.net/posts/a-brief-overview-of-python-java-bridges-in-2020.html

https://github.com/ninia/jep/wiki/FAQ

https://jpype.readthedocs.io/en/latest/userguide.html

关闭