使用 python 中的 java 库

发布于 2024-07-12 07:02:37 字数 438 浏览 1 评论 0原文

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

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

有什么指点吗? (仅供参考,我是 Python 新手)

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

因此,从 java 应用程序调用 python 应用程序是没有意义的,而且我无法在 jython 环境(在 JVM 上)中运行 py 应用程序。

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

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

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

Any pointers?
(FYI I'm v. new to 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.

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).

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

┊风居住的梦幻卍 2024-07-19 07:02:37

很抱歉重新启动该线程,但没有可接受的答案...

您也可以使用 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

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

免责声明:我是 Py4J 的作者

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:

>>> 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.

Disclaimer: I am the author of Py4J

七分※倦醒 2024-07-19 07:02:37

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

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.

土豪我们做朋友吧 2024-07-19 07:02:37

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

因此,您不会在虚拟机启动中浪费时间,并且可以更加面向服务。

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

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

  • json (请参阅 www.json.org)
  • rmi (Python :JPype)
  • REST
  • SOAP(仅适用于勇敢者)

选择您或您的伴侣熟悉的东西!

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

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

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

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

  • json (see www.json.org)
  • rmi (Python: JPype)
  • REST
  • SOAP (only for the brave)

Choose something you or your partners are familliar with!

海拔太高太耀眼 2024-07-19 07:02:37

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

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.

冷心人i 2024-07-19 07:02:37

尝试一下 JCC http://pypi.python.org/pypi/JCC/2.1

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 风格的许可证发布。

从包装说明来看:

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

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

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

Give JCC a try 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.

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.

避讳 2024-07-19 07:02:37

您可以使用一个名为 BridgeService 的库,它(目前)将 Java 代码公开为 Rest API。

https://github.com/adobe/bridgeService

您可以将您的库放入项目本身或如果它是您的项目,您可以将bridgeService添加到您的项目中并使其可访问。

例如,如果您想发送对 java 类的调用(如果您按照 运行 DEMO ):

如果您通过 POST 调用发送以下有效负载 http://localhost:8080/call

{
  "callContent": {
    "call1": {
      "class": "com.adobe.campaign.tests.bridge.testdata.one.SimpleStaticMethods",
      "method": "methodAcceptingStringArgument",
      "args": [
        "A Real"
      ]
    }
  }
}

在上面的示例中,代码将执行类 com.adobe.campaign.tests.bridge.testdata.one.SimpleStaticMethods 中的方法 methodAcceptingStringArgument

返回值将是:

{
    "returnValues": {
        "call1": "A Real_Success"
    },
    "callDurations": {
        "call1": 7
    }
}

You can use a library called BridgeService that exposes (for now) Java code as Rest API.

https://github.com/adobe/bridgeService

You can either put your library in the project itself or if it is a project of your, you can add the bridgeService to your project and make it accessible.

For example if you want to send a call to the java class (This example will work if you launch the project as it is following Running a DEMO ):

If you send the following payload with the POST call http://localhost:8080/call:

{
  "callContent": {
    "call1": {
      "class": "com.adobe.campaign.tests.bridge.testdata.one.SimpleStaticMethods",
      "method": "methodAcceptingStringArgument",
      "args": [
        "A Real"
      ]
    }
  }
}

In the example abobe the code will execute the method methodAcceptingStringArgumentin the class com.adobe.campaign.tests.bridge.testdata.one.SimpleStaticMethods.

The return value will be :

{
    "returnValues": {
        "call1": "A Real_Success"
    },
    "callDurations": {
        "call1": 7
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文