使用 python 中的 java 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/476968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:07:56  来源:igfitidea点击:

Using a java library from python

javapythonjython

提问by Jim

I have a python app and java app. The python app generates input for the java app and invokes it on the command line.

我有一个 python 应用程序和 java 应用程序。python 应用程序为 java 应用程序生成输入并在命令行上调用它。

I'm sure there must be a more elegant solution to this; just like using JNI to invoke C code from Java.

我相信必须有一个更优雅的解决方案;就像使用 JNI 从 Java 调用 C 代码一样。

Any pointers? (FYI I'm v. new to Python)

任何指针?(仅供参考,我是 Python 新手)

Clarification(at the cost of a long question: apologies) The py app (which I don't own) takes user input in the form of a number of configuration files. It then interprits these and farms work off to a number of (hidden) tools via a plugin mechanism. I'm looking to add support for the functionality provided by the legacy Java app.

澄清(以一个长问题为代价:道歉)py 应用程序(我不拥有)以许多配置文件的形式接受用户输入。然后它通过插件机制将这些和农场的工作转换为许多(隐藏的)工具。我希望添加对遗留 Java 应用程序提供的功能的支持。

So it doesn't make sense to call the python app from the java app and I can't run the py app in a jython environment (on the JVM).

所以从java应用程序调用python应用程序没有意义,我无法在jython环境(在JVM上)运行py应用程序。

Since there is no obvious mechanism for this I think the simple CL invocation is the best solution.

由于没有明显的机制,我认为简单的 CL 调用是最好的解决方案。

采纳答案by Barthelemy

Sorry to ressurect the thread, but there was no accepted answer...

很抱歉恢复线程,但没有接受的答案......

You could also use Py4J. There is an example on the frontpage and lots of documentation, but essentially, you just call Java methods from your python code as if they were python methods:

您也可以使用Py4J。首页上有一个示例和大量文档,但本质上,您只需从 Python 代码中调用 Java 方法,就好像它们是 Python 方法一样:

>>> from py4j.java_gateway import JavaGateway
>>> gateway = JavaGateway()                        # connect to the JVM
>>> java_object = gateway.jvm.mypackage.MyClass()  # invoke constructor
>>> other_object = java_object.doThat()
>>> other_object.doThis(1,'abc')
>>> gateway.jvm.java.lang.System.out.println('Hello World!') # call a static method

As opposed to Jython, Py4J runs in the Python VM so it is always "up to date" with the latest version of Python and you can use libraries that do not run well on Jython (e.g., lxml). The communication is done through sockets instead of JNI.

与 Jython 不同,Py4J 在 Python VM 中运行,因此它始终与最新版本的 Python 保持“同步”,您可以使用在 Jython 上运行不佳的库(例如,lxml)。通信是通过套接字而不是 JNI 完成的。

Disclaimer: I am the author of Py4J

免责声明:我是 Py4J 的作者

回答by Dave Ray

Take a look at Jython. It's kind of like JNI, but replace C with Python, i.e. you can call Python from Java and vice versa. It's not totally clear what you're trying to do or why your current approach isn't what you want.

看看Jython。它有点像 JNI,但用 Python 替换 C,即您可以从 Java 调用 Python,反之亦然。目前尚不清楚您正在尝试做什么或为什么您当前的方法不是您想要的。

回答by Blauohr

Wrap your Java-Code in a Container (Servlet / EJB).

将您的 Java 代码包装在容器(Servlet / EJB)中。

So you don′t loose time in the vm-startup and you go the way to more service-oriented.

因此,您不会在 vm-startup 中浪费时间,而是朝着更加面向服务的方向发展。

For the wraping you can use jython (only make sense if you are familiar with python)

对于包装,您可以使用 jython (仅当您熟悉 python 时才有意义

Choose a communication-protocoll in which python and java can use:

选择 python 和 java 可以使用的通信协议:

  • json (see www.json.org)
  • rmi (Python: JPype)
  • REST
  • SOAP (only for the brave)
  • json(参见 www.json.org)
  • rmi (Python: JPype)
  • 休息
  • 肥皂(只为勇敢者)

Choose something you or your partners are familliar with!

选择您或您的合作伙伴熟悉的东西!

回答by Stéphane Bonniez

If you really want to embed your Java app within your Python process, have a look at JPype. It provides access to Java through JNI.

如果您真的想将 Java 应用程序嵌入到 Python 进程中,请查看JPype。它通过 JNI 提供对 Java 的访问。

回答by Stéphane Bonniez

How about using swig: http://www.swig.org/Doc1.3/Java.html?

使用 swig 怎么样:http://www.swig.org/Doc1.3/Java.html ?

回答by sstock

Give JCC a try http://pypi.python.org/pypi/JCC/2.1

试试 JCC http://pypi.python.org/pypi/JCC/2.1

JCC is a code generator for calling Java directly from CPython. It supports CPython 2.3+, several JREs (Sun JDK 1.4+, Apple JRE 1.4+, and OpenJDK 1.7) on OS X, Linux, Solaris, and Windows. It's produced by the Open Source Application Foundation (OSAF, the people making Chandler) and is released under an Apache-style license.

JCC 是一个代码生成器,用于直接从 CPython 调用 Java。它支持 OS X、Linux、Solaris 和 Windows 上的 CPython 2.3+、多个 JRE(Sun JDK 1.4+、Apple JRE 1.4+ 和 OpenJDK 1.7)。它由开源应用程序基金会(OSAF,创建 Chandler 的人)制作,并在 Apache 风格的许可下发布。

From the package description:

从包描述:

JCC is a C++ code generator for producing the glue code necessary to call into Java classes from CPython via Java's Native Invocation Interface (JNI).

JCC generates C++ wrapper classes that hide all the gory details of JNI access as well Java memory and object reference management.

JCC generates CPython types that make these C++ classes accessible from a Python interpreter. JCC attempts to make these Python types pythonic by detecting iterators and property accessors. Iterators and mappings may also be declared to JCC.

JCC 是一个 C++ 代码生成器,用于生成通过 Java 的本机调用接口 (JNI) 从 CPython 调用 Java 类所需的粘合代码。

JCC 生成 C++ 包装类,这些类隐藏了 JNI 访问以及 Java 内存和对象引用管理的所有血腥细节。

JCC 生成 CPython 类型,使这些 C++ 类可以从 Python 解释器访问。JCC 试图通过检测迭代器和属性访问器来使这些 Python 类型变得 Pythonic。也可以向 JCC 声明迭代器和映射。