java可以使用python的库函数,使用python中的java库

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.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值