从Java调用Python模块 [英] Invoke Python modules from Java

查看:126
本文介绍了从Java调用Python模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C - igraph(图书馆名称)编写的图形库的Python接口。我需要从Java代码调用与此图库有关的python模块。就像这样,图书馆的核心在于c。这个核心已经导入到Python中,并且嵌入在核心中的函数的接口在Python中可用。我的项目的其余代码是Java,因此我也想用Java调用图形函数。
Jython - 它允许你用Java调用python模块是一个选项。我继续尝试Jython发现它在我的情况下不起作用,因为核心代码在C中并且Jython不支持任何导入为python代码中的ac dll。我还想过选择直接在c中调用图例程的方法。那是没有通过Python代码。我假设必须有一些东西可以让你从Java调用c代码,我怎么也不擅长C因此我没有去做。
我的最后一招似乎是使用Java从命令行执行Python解释器。但这是一个肮脏和无耻的。另外,要处理Python代码生成的结果,我必须将结果写入文件并在java中读回。又脏了。
有没有人能建议我的东西?感谢每个人给予时间。

I have a Python interface of a graph library written in C - igraph (the name of library). My need is to invoke the python modules pertaining to this graph library from Java code. It goes like this, the core of library is in c. This core has been imported into Python and interfaces to the functions embedded in core are available in Python. My project's rest of the code is in Java and hence I would like to call the graph functions by Java as well. Jython - which lets you invoke python modules with in Java was an option.I went on trying Jython to discover that it will not work in my case as the core code is in C and Jython wont support anything that is imported as a c dll in python code.I also thought of opting for the approach of calling graph routines directly in c. That is without passing through Python code. I am assuming there must be something which lets you call c code from Java, how ever I am not good in C hence I did not go for it. My last resort seems to execute Python interpreter from command line using Java. But that is a dirty and shameless. Also to deal with the results produced by Python code I will have to write the results in a file and read it back in java. Again dirty way. Is there something that any one can suggest me? Thanks to every one giving time.

感谢Igal回答。我看了一眼。乍一看,它似乎只是简单地调用python脚本。

Thanks Igal for answering. I had a look at it. At first glance it appears as if it is simply calling the python script.

Jep jep = new Jep(false, SCRIPT_PATH, cl);
jep.set("query", query);
jep.runScript(SCRIPT_PATH + file);
jep.close();

它与我们从命令行通过Java代码调用python解释器时的操作非常相似。

Isnt it very similar to what we would do if called the python interpreter from command line through a Java code.

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("python test.py");

关注的是如何使用Python脚本生成的结果。天真的方法是将它们写入文件并用Java读回。我正在寻找一种更聪明的方法。无论如何,谢谢你提出建议。

Concern is how do I use the results generated by Python script. The naive way is to write them to file and read it back in Java. I am searching for a smarter approach.Thanks for suggestion anyway.

推荐答案

如果你想从Java调用C函数, JNA (Java Native Access)可能就是这样。 JNA允许您在本机库中调用函数而无需编写C glue代码(就像使用JNI时一样),并自动映射Java和C中的原始数据类型。一个简单的示例可能如下所示:

If you want to call C functions from Java, JNA (Java Native Access) is probably the way to go. JNA allows you to call functions in native libraries without having to write the C glue code (as you would have to when using JNI), and automatically maps between primitive data types in Java and C. A simple example might look like this:

import com.sun.jna.Native;
import com.sun.jna.Library;

public class PrintfWrapper {
    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary)Native.loadLibrary("c", CLibrary.class);
        void printf(String formatString, Object... args);
    }

    public static void main(String[] args) {
        CLibrary.INSTANCE.printf("Hello, world\n");
    }
}

然而,因为igraph使用igraph会使事情变得复杂许多数据结构无法直接映射到它们的Java对应物中。有一个名为 JNAerator 的项目应该能够从igraph的头文件生成JNA源,但我有从来没有尝试过,结果仍然需要一些手动调整。

However, things will get complicated with igraph because igraph uses many data structures that cannot be mapped directly into their Java counterparts. There is a project called JNAerator which should be able to generate the JNA source from igraph's header files, but I have never tried it and chances are that the results will still need some manual tweaking.

另请注意 igraph的Java接口正在缓慢而稳定地发展,它可能会在几个月左右变得有用。

Also note that a Java interface for igraph is being developed slowly but steadily and it might become useful in a few months or so.

这篇关于从Java调用Python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆