使用 python 中的 java 库
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
很抱歉重新启动该线程,但没有可接受的答案...
您也可以使用 Py4J。 首页上有一个示例和大量文档,但本质上,您只需从 Python 代码中调用 Java 方法,就好像它们是 Python 方法一样:
与 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:
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
查看 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.
将 Java 代码包装在容器(Servlet / EJB)中。
因此,您不会在虚拟机启动中浪费时间,并且可以更加面向服务。
对于包装,您可以使用 jython (仅当您熟悉 python 时才有意义)
选择 python 和 java 可以使用的通信协议:
选择您或您的伴侣熟悉的东西!
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:
Choose something you or your partners are familliar with!
如果您确实想将 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.
使用 swig 怎么样: http://www.swig.org/Doc1.3/Java .html ?
How about using swig: http://www.swig.org/Doc1.3/Java.html ?
尝试一下 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 风格的许可证发布。
从包装说明来看:
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:
您可以使用一个名为 BridgeService 的库,它(目前)将 Java 代码公开为 Rest API。
https://github.com/adobe/bridgeService
您可以将您的库放入项目本身或如果它是您的项目,您可以将bridgeService添加到您的项目中并使其可访问。
例如,如果您想发送对 java 类的调用(如果您按照 运行 DEMO ):
如果您通过 POST 调用发送以下有效负载
http://localhost:8080/call
:在上面的示例中,代码将执行类
com.adobe.campaign.tests.bridge.testdata.one.SimpleStaticMethods
中的方法methodAcceptingStringArgument
。返回值将是:
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
:In the example abobe the code will execute the method
methodAcceptingStringArgument
in the classcom.adobe.campaign.tests.bridge.testdata.one.SimpleStaticMethods
.The return value will be :